importer.nim 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 0 ..< sonsLen(etyp.n):
  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. let multiImport = s.kind notin ExportableSymKinds or s.kind in skProcKinds
  77. # for an enumeration we have to add all identifiers
  78. if multiImport:
  79. # for a overloadable syms add all overloaded routines
  80. var it: TIdentIter
  81. var e = initIdentIter(it, fromMod.tab, s.name)
  82. while e != nil:
  83. if e.name.id != s.name.id: internalError(c.config, n.info, "importSymbol: 3")
  84. if s.kind in ExportableSymKinds:
  85. rawImportSymbol(c, e)
  86. e = nextIdentIter(it, fromMod.tab)
  87. else:
  88. rawImportSymbol(c, s)
  89. suggestSym(c.config, n.info, s, c.graph.usageSym, false)
  90. proc importAllSymbolsExcept(c: PContext, fromMod: PSym, exceptSet: IntSet) =
  91. var i: TTabIter
  92. var s = initTabIter(i, fromMod.tab)
  93. while s != nil:
  94. if s.kind != skModule:
  95. if s.kind != skEnumField:
  96. if s.kind notin ExportableSymKinds:
  97. internalError(c.config, s.info, "importAllSymbols: " & $s.kind & " " & s.name.s)
  98. if exceptSet.isNil or s.name.id notin exceptSet:
  99. rawImportSymbol(c, s)
  100. s = nextIter(i, fromMod.tab)
  101. proc importAllSymbols*(c: PContext, fromMod: PSym) =
  102. var exceptSet: IntSet
  103. importAllSymbolsExcept(c, fromMod, exceptSet)
  104. proc importForwarded(c: PContext, n: PNode, exceptSet: IntSet) =
  105. if n.isNil: return
  106. case n.kind
  107. of nkExportStmt:
  108. for a in n:
  109. assert a.kind == nkSym
  110. let s = a.sym
  111. if s.kind == skModule:
  112. importAllSymbolsExcept(c, s, exceptSet)
  113. elif exceptSet.isNil or s.name.id notin exceptSet:
  114. rawImportSymbol(c, s)
  115. of nkExportExceptStmt:
  116. localError(c.config, n.info, "'export except' not implemented")
  117. else:
  118. for i in 0..safeLen(n)-1:
  119. importForwarded(c, n.sons[i], exceptSet)
  120. proc importModuleAs(c: PContext; n: PNode, realModule: PSym): PSym =
  121. result = realModule
  122. if n.kind != nkImportAs: discard
  123. elif n.len != 2 or n.sons[1].kind != nkIdent:
  124. localError(c.config, n.info, "module alias must be an identifier")
  125. elif n.sons[1].ident.id != realModule.name.id:
  126. # some misguided guy will write 'import abc.foo as foo' ...
  127. result = createModuleAlias(realModule, n.sons[1].ident, realModule.info,
  128. c.config.options)
  129. proc myImportModule(c: PContext, n: PNode; importStmtResult: PNode): PSym =
  130. let f = checkModuleName(c.config, n)
  131. if f != InvalidFileIdx:
  132. let L = c.graph.importStack.len
  133. let recursion = c.graph.importStack.find(f)
  134. c.graph.importStack.add f
  135. #echo "adding ", toFullPath(f), " at ", L+1
  136. if recursion >= 0:
  137. var err = ""
  138. for i in recursion ..< L:
  139. if i > recursion: err.add "\n"
  140. err.add toFullPath(c.config, c.graph.importStack[i]) & " imports " &
  141. toFullPath(c.config, c.graph.importStack[i+1])
  142. c.recursiveDep = err
  143. result = importModuleAs(c, n, c.graph.importModuleCallback(c.graph, c.module, f))
  144. #echo "set back to ", L
  145. c.graph.importStack.setLen(L)
  146. # we cannot perform this check reliably because of
  147. # test: modules/import_in_config)
  148. when true:
  149. if result.info.fileIndex == c.module.info.fileIndex and
  150. result.info.fileIndex == n.info.fileIndex:
  151. localError(c.config, n.info, "A module cannot import itself")
  152. if sfDeprecated in result.flags:
  153. if result.constraint != nil:
  154. message(c.config, n.info, warnDeprecated, result.constraint.strVal & "; " & result.name.s & " is deprecated")
  155. else:
  156. message(c.config, n.info, warnDeprecated, result.name.s & " is deprecated")
  157. suggestSym(c.config, n.info, result, c.graph.usageSym, false)
  158. importStmtResult.add newSymNode(result, n.info)
  159. #newStrNode(toFullPath(c.config, f), n.info)
  160. proc transformImportAs(c: PContext; n: PNode): PNode =
  161. if n.kind == nkInfix and considerQuotedIdent(c, n[0]).s == "as":
  162. result = newNodeI(nkImportAs, n.info)
  163. result.add n.sons[1]
  164. result.add n.sons[2]
  165. else:
  166. result = n
  167. proc impMod(c: PContext; it: PNode; importStmtResult: PNode) =
  168. let it = transformImportAs(c, it)
  169. let m = myImportModule(c, it, importStmtResult)
  170. if m != nil:
  171. var emptySet: IntSet
  172. # ``addDecl`` needs to be done before ``importAllSymbols``!
  173. addDecl(c, m, it.info) # add symbol to symbol table of module
  174. importAllSymbolsExcept(c, m, emptySet)
  175. #importForwarded(c, m.ast, emptySet)
  176. proc evalImport*(c: PContext, n: PNode): PNode =
  177. result = newNodeI(nkImportStmt, n.info)
  178. for i in 0 ..< sonsLen(n):
  179. let it = n.sons[i]
  180. if it.kind == nkInfix and it.len == 3 and it[2].kind == nkBracket:
  181. let sep = it[0]
  182. let dir = it[1]
  183. var imp = newNodeI(nkInfix, it.info)
  184. imp.add sep
  185. imp.add dir
  186. imp.add sep # dummy entry, replaced in the loop
  187. for x in it[2]:
  188. # transform `a/b/[c as d]` to `/a/b/c as d`
  189. if x.kind == nkInfix and x.sons[0].ident.s == "as":
  190. let impAs = copyTree(x)
  191. imp.sons[2] = x.sons[1]
  192. impAs.sons[1] = imp
  193. impMod(c, imp, result)
  194. else:
  195. imp.sons[2] = x
  196. impMod(c, imp, result)
  197. else:
  198. impMod(c, it, result)
  199. proc evalFrom*(c: PContext, n: PNode): PNode =
  200. result = newNodeI(nkImportStmt, n.info)
  201. checkMinSonsLen(n, 2, c.config)
  202. n.sons[0] = transformImportAs(c, n.sons[0])
  203. var m = myImportModule(c, n.sons[0], result)
  204. if m != nil:
  205. n.sons[0] = newSymNode(m)
  206. addDecl(c, m, n.info) # add symbol to symbol table of module
  207. for i in 1 ..< sonsLen(n):
  208. if n.sons[i].kind != nkNilLit:
  209. importSymbol(c, n.sons[i], m)
  210. proc evalImportExcept*(c: PContext, n: PNode): PNode =
  211. result = newNodeI(nkImportStmt, n.info)
  212. checkMinSonsLen(n, 2, c.config)
  213. n.sons[0] = transformImportAs(c, n.sons[0])
  214. var m = myImportModule(c, n.sons[0], result)
  215. if m != nil:
  216. n.sons[0] = newSymNode(m)
  217. addDecl(c, m, n.info) # add symbol to symbol table of module
  218. importAllSymbolsExcept(c, m, readExceptSet(c, n))
  219. #importForwarded(c, m.ast, exceptSet)