vmdeps.nim 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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), 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. # keep original type info for getType calls on the output node:
  126. result.typ = t
  127. else:
  128. result = newNodeX(nkBracketExpr)
  129. #result.add mapTypeToAst(t.lastSon, info)
  130. result.add mapTypeToAst(t[0], info)
  131. for i in 1..<t.len-1:
  132. result.add mapTypeToAst(t[i], info)
  133. else:
  134. result = mapTypeToAstX(cache, t.lastSon, info, idgen, inst, allowRecursion)
  135. # keep original type info for getType calls on the output node:
  136. result.typ = t
  137. of tyGenericBody:
  138. if inst:
  139. result = mapTypeToAstR(t.lastSon, info)
  140. else:
  141. result = mapTypeToAst(t.lastSon, info)
  142. of tyAlias:
  143. result = mapTypeToAstX(cache, t.lastSon, info, idgen, inst, allowRecursion)
  144. of tyOrdinal:
  145. result = mapTypeToAst(t.lastSon, info)
  146. of tyDistinct:
  147. if inst:
  148. result = newNodeX(nkDistinctTy)
  149. result.add mapTypeToAst(t[0], info)
  150. else:
  151. if allowRecursion or t.sym == nil:
  152. result = mapTypeToBracket("distinct", mDistinct, t, info)
  153. else:
  154. result = atomicType(t.sym)
  155. of tyGenericParam, tyForward:
  156. result = atomicType(t.sym)
  157. of tyObject:
  158. if inst:
  159. result = newNodeX(nkObjectTy)
  160. var objectDef = t.sym.ast[2]
  161. if objectDef.kind == nkRefTy:
  162. objectDef = objectDef[0]
  163. result.add objectDef[0].copyTree # copy object pragmas
  164. if t[0] == nil:
  165. result.add newNodeI(nkEmpty, info)
  166. else: # handle parent object
  167. var nn = newNodeX(nkOfInherit)
  168. nn.add mapTypeToAst(t[0], info)
  169. result.add nn
  170. if t.n.len > 0:
  171. result.add objectNode(cache, t.n, idgen)
  172. else:
  173. result.add newNodeI(nkEmpty, info)
  174. else:
  175. if allowRecursion or t.sym == nil:
  176. result = newNodeIT(nkObjectTy, if t.n.isNil: info else: t.n.info, t)
  177. result.add newNodeI(nkEmpty, info)
  178. if t[0] == nil:
  179. result.add newNodeI(nkEmpty, info)
  180. else:
  181. result.add mapTypeToAst(t[0], info)
  182. result.add copyTree(t.n)
  183. else:
  184. result = atomicType(t.sym)
  185. of tyEnum:
  186. result = newNodeIT(nkEnumTy, if t.n.isNil: info else: t.n.info, t)
  187. result.add newNodeI(nkEmpty, info) # pragma node, currently always empty for enum
  188. for c in t.n.sons:
  189. result.add copyTree(c)
  190. of tyTuple:
  191. if inst:
  192. # only named tuples have a node, unnamed tuples don't
  193. if t.n.isNil:
  194. result = newNodeX(nkTupleConstr)
  195. for subType in t:
  196. result.add mapTypeToAst(subType, info)
  197. else:
  198. result = newNodeX(nkTupleTy)
  199. for s in t.n.sons:
  200. result.add newIdentDefs(s)
  201. else:
  202. result = mapTypeToBracket("tuple", mTuple, t, info)
  203. of tySet: result = mapTypeToBracket("set", mSet, t, info)
  204. of tyPtr:
  205. if inst:
  206. result = newNodeX(nkPtrTy)
  207. result.add mapTypeToAst(t[0], info)
  208. else:
  209. result = mapTypeToBracket("ptr", mPtr, t, info)
  210. of tyRef:
  211. if inst:
  212. result = newNodeX(nkRefTy)
  213. result.add mapTypeToAst(t[0], info)
  214. else:
  215. result = mapTypeToBracket("ref", mRef, t, info)
  216. of tyVar:
  217. if inst:
  218. result = newNodeX(nkVarTy)
  219. result.add mapTypeToAst(t[0], info)
  220. else:
  221. result = mapTypeToBracket("var", mVar, t, info)
  222. of tyLent: result = mapTypeToBracket("lent", mBuiltinType, t, info)
  223. of tySink: result = mapTypeToBracket("sink", mBuiltinType, t, info)
  224. of tySequence: result = mapTypeToBracket("seq", mSeq, t, info)
  225. of tyProc:
  226. if inst:
  227. result = newNodeX(nkProcTy)
  228. var fp = newNodeX(nkFormalParams)
  229. if t[0] == nil:
  230. fp.add newNodeI(nkEmpty, info)
  231. else:
  232. fp.add mapTypeToAst(t[0], t.n[0].info)
  233. for i in 1..<t.len:
  234. fp.add newIdentDefs(t.n[i], t[i])
  235. result.add fp
  236. result.add if t.n[0].len > 0: t.n[0][pragmasEffects].copyTree
  237. else: newNodeI(nkEmpty, info)
  238. else:
  239. result = mapTypeToBracket("proc", mNone, t, info)
  240. of tyOpenArray: result = mapTypeToBracket("openArray", mOpenArray, t, info)
  241. of tyRange:
  242. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  243. result.add atomicType("range", mRange)
  244. if inst and t.n.len == 2:
  245. let rng = newNodeX(nkInfix)
  246. rng.add newIdentNode(getIdent(cache, ".."), info)
  247. rng.add t.n[0].copyTree
  248. rng.add t.n[1].copyTree
  249. result.add rng
  250. else:
  251. result.add t.n[0].copyTree
  252. if t.n.len > 1:
  253. result.add t.n[1].copyTree
  254. of tyPointer: result = atomicType("pointer", mPointer)
  255. of tyString: result = atomicType("string", mString)
  256. of tyCstring: result = atomicType("cstring", mCstring)
  257. of tyInt: result = atomicType("int", mInt)
  258. of tyInt8: result = atomicType("int8", mInt8)
  259. of tyInt16: result = atomicType("int16", mInt16)
  260. of tyInt32: result = atomicType("int32", mInt32)
  261. of tyInt64: result = atomicType("int64", mInt64)
  262. of tyFloat: result = atomicType("float", mFloat)
  263. of tyFloat32: result = atomicType("float32", mFloat32)
  264. of tyFloat64: result = atomicType("float64", mFloat64)
  265. of tyFloat128: result = atomicType("float128", mFloat128)
  266. of tyUInt: result = atomicType("uint", mUInt)
  267. of tyUInt8: result = atomicType("uint8", mUInt8)
  268. of tyUInt16: result = atomicType("uint16", mUInt16)
  269. of tyUInt32: result = atomicType("uint32", mUInt32)
  270. of tyUInt64: result = atomicType("uint64", mUInt64)
  271. of tyVarargs: result = mapTypeToBracket("varargs", mVarargs, t, info)
  272. of tyProxy: result = atomicType("error", mNone)
  273. of tyBuiltInTypeClass:
  274. result = mapTypeToBracket("builtinTypeClass", mNone, t, info)
  275. of tyUserTypeClass, tyUserTypeClassInst:
  276. if t.isResolvedUserTypeClass:
  277. result = mapTypeToAst(t.lastSon, info)
  278. else:
  279. result = mapTypeToBracket("concept", mNone, t, info)
  280. result.add t.n.copyTree
  281. of tyCompositeTypeClass:
  282. result = mapTypeToBracket("compositeTypeClass", mNone, t, info)
  283. of tyAnd: result = mapTypeToBracket("and", mAnd, t, info)
  284. of tyOr: result = mapTypeToBracket("or", mOr, t, info)
  285. of tyNot: result = mapTypeToBracket("not", mNot, t, info)
  286. of tyIterable: result = mapTypeToBracket("iterable", mIterableType, t, info)
  287. of tyAnything: result = atomicType("anything", mNone)
  288. of tyInferred: result = mapTypeToAstX(cache, t.lastSon, info, idgen, inst, allowRecursion)
  289. of tyStatic, tyFromExpr:
  290. if inst:
  291. if t.n != nil: result = t.n.copyTree
  292. else: result = atomicType("void", mVoid)
  293. else:
  294. result = newNodeIT(nkBracketExpr, if t.n.isNil: info else: t.n.info, t)
  295. result.add atomicType("static", mNone)
  296. if t.n != nil:
  297. result.add t.n.copyTree
  298. of tyOwned: result = mapTypeToBracket("owned", mBuiltinType, t, info)
  299. of tyConcept:
  300. result = mapTypeToBracket("concept", mNone, t, info)
  301. result.add t.n.copyTree
  302. proc opMapTypeToAst*(cache: IdentCache; t: PType; info: TLineInfo; idgen: IdGenerator): PNode =
  303. result = mapTypeToAstX(cache, t, info, idgen, inst=false, allowRecursionX=true)
  304. # the "Inst" version includes generic parameters in the resulting type tree
  305. # and also tries to look like the corresponding Nim type declaration
  306. proc opMapTypeInstToAst*(cache: IdentCache; t: PType; info: TLineInfo; idgen: IdGenerator): PNode =
  307. result = mapTypeToAstX(cache, t, info, idgen, inst=true, allowRecursionX=false)
  308. # the "Impl" version includes generic parameters in the resulting type tree
  309. # and also tries to look like the corresponding Nim type implementation
  310. proc opMapTypeImplToAst*(cache: IdentCache; t: PType; info: TLineInfo; idgen: IdGenerator): PNode =
  311. result = mapTypeToAstX(cache, t, info, idgen, inst=true, allowRecursionX=true)