vmdeps.nim 11 KB

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