vmdeps.nim 12 KB

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