importer.nim 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements the symbol importing mechanism.
  10. import
  11. intsets, strutils, os, ast, astalgo, msgs, options, idents, lookups,
  12. semdata, passes, renderer, modulepaths, sigmatch, lineinfos
  13. proc readExceptSet*(c: PContext, n: PNode): IntSet =
  14. assert n.kind in {nkImportExceptStmt, nkExportExceptStmt}
  15. result = initIntSet()
  16. for i in 1 ..< n.len:
  17. let ident = lookups.considerQuotedIdent(c, n[i])
  18. result.incl(ident.id)
  19. proc importPureEnumField*(c: PContext; s: PSym) =
  20. let check = strTableGet(c.importTable.symbols, s.name)
  21. if check == nil:
  22. let checkB = strTableGet(c.pureEnumFields, s.name)
  23. if checkB == nil:
  24. strTableAdd(c.pureEnumFields, s)
  25. else:
  26. # mark as ambigous:
  27. incl(c.ambiguousSymbols, checkB.id)
  28. incl(c.ambiguousSymbols, s.id)
  29. proc rawImportSymbol(c: PContext, s: PSym) =
  30. # This does not handle stubs, because otherwise loading on demand would be
  31. # pointless in practice. So importing stubs is fine here!
  32. # check if we have already a symbol of the same name:
  33. var check = strTableGet(c.importTable.symbols, s.name)
  34. if check != nil and check.id != s.id:
  35. if s.kind notin OverloadableSyms or check.kind notin OverloadableSyms:
  36. # s and check need to be qualified:
  37. incl(c.ambiguousSymbols, s.id)
  38. incl(c.ambiguousSymbols, check.id)
  39. # thanks to 'export' feature, it could be we import the same symbol from
  40. # multiple sources, so we need to call 'StrTableAdd' here:
  41. strTableAdd(c.importTable.symbols, s)
  42. if s.kind == skType:
  43. var etyp = s.typ
  44. if etyp.kind in {tyBool, tyEnum}:
  45. for j in countup(0, sonsLen(etyp.n) - 1):
  46. var e = etyp.n.sons[j].sym
  47. if e.kind != skEnumField:
  48. internalError(c.config, s.info, "rawImportSymbol")
  49. # BUGFIX: because of aliases for enums the symbol may already
  50. # have been put into the symbol table
  51. # BUGFIX: but only iff they are the same symbols!
  52. var it: TIdentIter
  53. check = initIdentIter(it, c.importTable.symbols, e.name)
  54. while check != nil:
  55. if check.id == e.id:
  56. e = nil
  57. break
  58. check = nextIdentIter(it, c.importTable.symbols)
  59. if e != nil:
  60. if sfPure notin s.flags:
  61. rawImportSymbol(c, e)
  62. else:
  63. importPureEnumField(c, e)
  64. else:
  65. # rodgen assures that converters and patterns are no stubs
  66. if s.kind == skConverter: addConverter(c, s)
  67. if hasPattern(s): addPattern(c, s)
  68. proc importSymbol(c: PContext, n: PNode, fromMod: PSym) =
  69. let ident = lookups.considerQuotedIdent(c, n)
  70. let s = strTableGet(fromMod.tab, ident)
  71. if s == nil:
  72. errorUndeclaredIdentifier(c, n.info, ident.s)
  73. else:
  74. when false:
  75. if s.kind == skStub: loadStub(s)
  76. if s.kind notin ExportableSymKinds:
  77. internalError(c.config, n.info, "importSymbol: 2")
  78. # for an enumeration we have to add all identifiers
  79. case s.kind
  80. of skProcKinds:
  81. # for a overloadable syms add all overloaded routines
  82. var it: TIdentIter
  83. var e = initIdentIter(it, fromMod.tab, s.name)
  84. while e != nil:
  85. if e.name.id != s.name.id: internalError(c.config, n.info, "importSymbol: 3")
  86. rawImportSymbol(c, e)
  87. e = nextIdentIter(it, fromMod.tab)
  88. else: rawImportSymbol(c, s)
  89. proc importAllSymbolsExcept(c: PContext, fromMod: PSym, exceptSet: IntSet) =
  90. var i: TTabIter
  91. var s = initTabIter(i, fromMod.tab)
  92. while s != nil:
  93. if s.kind != skModule:
  94. if s.kind != skEnumField:
  95. if s.kind notin ExportableSymKinds:
  96. internalError(c.config, s.info, "importAllSymbols: " & $s.kind)
  97. if exceptSet.isNil or s.name.id notin exceptSet:
  98. rawImportSymbol(c, s)
  99. s = nextIter(i, fromMod.tab)
  100. proc importAllSymbols*(c: PContext, fromMod: PSym) =
  101. var exceptSet: IntSet
  102. importAllSymbolsExcept(c, fromMod, exceptSet)
  103. proc importForwarded(c: PContext, n: PNode, exceptSet: IntSet) =
  104. if n.isNil: return
  105. case n.kind
  106. of nkExportStmt:
  107. for a in n:
  108. assert a.kind == nkSym
  109. let s = a.sym
  110. if s.kind == skModule:
  111. importAllSymbolsExcept(c, s, exceptSet)
  112. elif exceptSet.isNil or s.name.id notin exceptSet:
  113. rawImportSymbol(c, s)
  114. of nkExportExceptStmt:
  115. localError(c.config, n.info, "'export except' not implemented")
  116. else:
  117. for i in 0..safeLen(n)-1:
  118. importForwarded(c, n.sons[i], exceptSet)
  119. proc importModuleAs(c: PContext; n: PNode, realModule: PSym): PSym =
  120. result = realModule
  121. if n.kind != nkImportAs: discard
  122. elif n.len != 2 or n.sons[1].kind != nkIdent:
  123. localError(c.config, n.info, "module alias must be an identifier")
  124. elif n.sons[1].ident.id != realModule.name.id:
  125. # some misguided guy will write 'import abc.foo as foo' ...
  126. result = createModuleAlias(realModule, n.sons[1].ident, realModule.info,
  127. c.config.options)
  128. proc myImportModule(c: PContext, n: PNode; importStmtResult: PNode): PSym =
  129. let f = checkModuleName(c.config, n)
  130. if f != InvalidFileIDX:
  131. let L = c.graph.importStack.len
  132. let recursion = c.graph.importStack.find(f)
  133. c.graph.importStack.add f
  134. #echo "adding ", toFullPath(f), " at ", L+1
  135. if recursion >= 0:
  136. var err = ""
  137. for i in countup(recursion, L-1):
  138. if i > recursion: err.add "\n"
  139. err.add toFullPath(c.config, c.graph.importStack[i]) & " imports " &
  140. toFullPath(c.config, c.graph.importStack[i+1])
  141. c.recursiveDep = err
  142. result = importModuleAs(c, n, c.graph.importModuleCallback(c.graph, c.module, f))
  143. #echo "set back to ", L
  144. c.graph.importStack.setLen(L)
  145. # we cannot perform this check reliably because of
  146. # test: modules/import_in_config)
  147. when true:
  148. if result.info.fileIndex == c.module.info.fileIndex and
  149. result.info.fileIndex == n.info.fileIndex:
  150. localError(c.config, n.info, "A module cannot import itself")
  151. if sfDeprecated in result.flags:
  152. if result.constraint != nil:
  153. message(c.config, n.info, warnDeprecated, result.constraint.strVal & "; " & result.name.s)
  154. else:
  155. message(c.config, n.info, warnDeprecated, result.name.s)
  156. suggestSym(c.config, n.info, result, c.graph.usageSym, false)
  157. importStmtResult.add newSymNode(result, n.info)
  158. #newStrNode(toFullPath(c.config, f), n.info)
  159. proc transformImportAs(c: PContext; n: PNode): PNode =
  160. if n.kind == nkInfix and considerQuotedIdent(c, n[0]).s == "as":
  161. result = newNodeI(nkImportAs, n.info)
  162. result.add n.sons[1]
  163. result.add n.sons[2]
  164. else:
  165. result = n
  166. proc impMod(c: PContext; it: PNode; importStmtResult: PNode) =
  167. let it = transformImportAs(c, it)
  168. let m = myImportModule(c, it, importStmtResult)
  169. if m != nil:
  170. var emptySet: IntSet
  171. # ``addDecl`` needs to be done before ``importAllSymbols``!
  172. addDecl(c, m, it.info) # add symbol to symbol table of module
  173. importAllSymbolsExcept(c, m, emptySet)
  174. #importForwarded(c, m.ast, emptySet)
  175. proc evalImport*(c: PContext, n: PNode): PNode =
  176. result = newNodeI(nkImportStmt, n.info)
  177. for i in countup(0, sonsLen(n) - 1):
  178. let it = n.sons[i]
  179. if it.kind == nkInfix and it.len == 3 and it[2].kind == nkBracket:
  180. let sep = it[0]
  181. let dir = it[1]
  182. var imp = newNodeI(nkInfix, it.info)
  183. imp.add sep
  184. imp.add dir
  185. imp.add sep # dummy entry, replaced in the loop
  186. for x in it[2]:
  187. # transform `a/b/[c as d]` to `/a/b/c as d`
  188. if x.kind == nkInfix and x.sons[0].ident.s == "as":
  189. let impAs = copyTree(x)
  190. imp.sons[2] = x.sons[1]
  191. impAs.sons[1] = imp
  192. impMod(c, imp, result)
  193. else:
  194. imp.sons[2] = x
  195. impMod(c, imp, result)
  196. else:
  197. impMod(c, it, result)
  198. proc evalFrom*(c: PContext, n: PNode): PNode =
  199. result = newNodeI(nkImportStmt, n.info)
  200. checkMinSonsLen(n, 2, c.config)
  201. n.sons[0] = transformImportAs(c, n.sons[0])
  202. var m = myImportModule(c, n.sons[0], result)
  203. if m != nil:
  204. n.sons[0] = newSymNode(m)
  205. addDecl(c, m, n.info) # add symbol to symbol table of module
  206. for i in countup(1, sonsLen(n) - 1):
  207. if n.sons[i].kind != nkNilLit:
  208. importSymbol(c, n.sons[i], m)
  209. proc evalImportExcept*(c: PContext, n: PNode): PNode =
  210. result = newNodeI(nkImportStmt, n.info)
  211. checkMinSonsLen(n, 2, c.config)
  212. n.sons[0] = transformImportAs(c, n.sons[0])
  213. var m = myImportModule(c, n.sons[0], result)
  214. if m != nil:
  215. n.sons[0] = newSymNode(m)
  216. addDecl(c, m, n.info) # add symbol to symbol table of module
  217. importAllSymbolsExcept(c, m, readExceptSet(c, n))
  218. #importForwarded(c, m.ast, exceptSet)