modules.nim 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. ## Implements the module handling, including the caching of modules.
  10. import
  11. ast, astalgo, magicsys, std / sha1, msgs, cgendata, sigmatch, options,
  12. idents, os, lexer, idgen, passes, syntaxes, llstream, modulegraphs, rod,
  13. lineinfos, pathutils, tables
  14. proc resetSystemArtifacts*(g: ModuleGraph) =
  15. magicsys.resetSysTypes(g)
  16. proc partialInitModule(result: PSym; graph: ModuleGraph; fileIdx: FileIndex; filename: AbsoluteFile) =
  17. let
  18. pck = getPackageName(graph.config, filename.string)
  19. pck2 = if pck.len > 0: pck else: "unknown"
  20. pack = getIdent(graph.cache, pck2)
  21. var packSym = graph.packageSyms.strTableGet(pack)
  22. if packSym == nil:
  23. packSym = newSym(skPackage, getIdent(graph.cache, pck2), nil, result.info)
  24. initStrTable(packSym.tab)
  25. graph.packageSyms.strTableAdd(packSym)
  26. else:
  27. let existing = strTableGet(packSym.tab, result.name)
  28. if existing != nil and existing.info.fileIndex != result.info.fileIndex:
  29. when false:
  30. # we used to produce an error:
  31. localError(graph.config, result.info,
  32. "module names need to be unique per Nimble package; module clashes with " &
  33. toFullPath(graph.config, existing.info.fileIndex))
  34. else:
  35. # but starting with version 0.20 we now produce a fake Nimble package instead
  36. # to resolve the conflicts:
  37. let pck3 = fakePackageName(graph.config, filename)
  38. packSym = newSym(skPackage, getIdent(graph.cache, pck3), nil, result.info)
  39. initStrTable(packSym.tab)
  40. graph.packageSyms.strTableAdd(packSym)
  41. result.owner = packSym
  42. result.position = int fileIdx
  43. if int(fileIdx) >= graph.modules.len:
  44. setLen(graph.modules, int(fileIdx) + 1)
  45. graph.modules[result.position] = result
  46. incl(result.flags, sfUsed)
  47. initStrTable(result.tab)
  48. strTableAdd(result.tab, result) # a module knows itself
  49. strTableAdd(packSym.tab, result)
  50. proc newModule(graph: ModuleGraph; fileIdx: FileIndex): PSym =
  51. # We cannot call ``newSym`` here, because we have to circumvent the ID
  52. # mechanism, which we do in order to assign each module a persistent ID.
  53. new(result)
  54. result.id = -1 # for better error checking
  55. result.kind = skModule
  56. let filename = AbsoluteFile toFullPath(graph.config, fileIdx)
  57. result.name = getIdent(graph.cache, splitFile(filename).name)
  58. if not isNimIdentifier(result.name.s):
  59. rawMessage(graph.config, errGenerated, "invalid module name: " & result.name.s)
  60. result.info = newLineInfo(fileIdx, 1, 1)
  61. partialInitModule(result, graph, fileIdx, filename)
  62. proc compileModule*(graph: ModuleGraph; fileIdx: FileIndex; flags: TSymFlags): PSym =
  63. result = graph.getModule(fileIdx)
  64. if result == nil:
  65. let filename = AbsoluteFile toFullPath(graph.config, fileIdx)
  66. let (r, id) = loadModuleSym(graph, fileIdx, filename)
  67. result = r
  68. if result == nil:
  69. result = newModule(graph, fileIdx)
  70. result.flags = result.flags + flags
  71. if sfMainModule in result.flags:
  72. graph.config.mainPackageId = result.owner.id
  73. result.id = id
  74. registerModule(graph, result)
  75. else:
  76. partialInitModule(result, graph, fileIdx, filename)
  77. result.id = id
  78. assert result.id < 0
  79. discard processModule(graph, result,
  80. if sfMainModule in flags and graph.config.projectIsStdin: stdin.llStreamOpen else: nil)
  81. elif graph.isDirty(result):
  82. result.flags.excl sfDirty
  83. # reset module fields:
  84. initStrTable(result.tab)
  85. result.ast = nil
  86. discard processModule(graph, result,
  87. if sfMainModule in flags and graph.config.projectIsStdin: stdin.llStreamOpen else: nil)
  88. graph.markClientsDirty(fileIdx)
  89. proc importModule*(graph: ModuleGraph; s: PSym, fileIdx: FileIndex): PSym {.procvar.} =
  90. # this is called by the semantic checking phase
  91. assert graph.config != nil
  92. result = compileModule(graph, fileIdx, {})
  93. graph.addDep(s, fileIdx)
  94. # keep track of import relationships
  95. if graph.config.hcrOn:
  96. graph.importDeps.mgetOrPut(FileIndex(s.position), @[]).add(fileIdx)
  97. #if sfSystemModule in result.flags:
  98. # localError(result.info, errAttemptToRedefine, result.name.s)
  99. # restore the notes for outer module:
  100. graph.config.notes =
  101. if s.owner.id == graph.config.mainPackageId or isDefined(graph.config, "booting"): graph.config.mainPackageNotes
  102. else: graph.config.foreignPackageNotes
  103. proc includeModule*(graph: ModuleGraph; s: PSym, fileIdx: FileIndex): PNode {.procvar.} =
  104. result = syntaxes.parseFile(fileIdx, graph.cache, graph.config)
  105. graph.addDep(s, fileIdx)
  106. graph.addIncludeDep(s.position.FileIndex, fileIdx)
  107. proc connectCallbacks*(graph: ModuleGraph) =
  108. graph.includeFileCallback = includeModule
  109. graph.importModuleCallback = importModule
  110. proc compileSystemModule*(graph: ModuleGraph) =
  111. if graph.systemModule == nil:
  112. connectCallbacks(graph)
  113. graph.config.m.systemFileIdx = fileInfoIdx(graph.config,
  114. graph.config.libpath / RelativeFile"system.nim")
  115. discard graph.compileModule(graph.config.m.systemFileIdx, {sfSystemModule})
  116. proc wantMainModule*(conf: ConfigRef) =
  117. if conf.projectFull.isEmpty:
  118. fatal(conf, newLineInfo(conf, AbsoluteFile"command line", 1, 1), errGenerated,
  119. "command expects a filename")
  120. conf.projectMainIdx = fileInfoIdx(conf, addFileExt(conf.projectFull, NimExt))
  121. proc compileProject*(graph: ModuleGraph; projectFileIdx = InvalidFileIDX) =
  122. connectCallbacks(graph)
  123. let conf = graph.config
  124. wantMainModule(conf)
  125. let systemFileIdx = fileInfoIdx(conf, conf.libpath / RelativeFile"system.nim")
  126. let projectFile = if projectFileIdx == InvalidFileIDX: conf.projectMainIdx else: projectFileIdx
  127. graph.importStack.add projectFile
  128. if projectFile == systemFileIdx:
  129. discard graph.compileModule(projectFile, {sfMainModule, sfSystemModule})
  130. else:
  131. graph.compileSystemModule()
  132. discard graph.compileModule(projectFile, {sfMainModule})
  133. proc makeModule*(graph: ModuleGraph; filename: AbsoluteFile): PSym =
  134. result = graph.newModule(fileInfoIdx(graph.config, filename))
  135. result.id = getID()
  136. registerModule(graph, result)
  137. proc makeModule*(graph: ModuleGraph; filename: string): PSym =
  138. result = makeModule(graph, AbsoluteFile filename)
  139. proc makeStdinModule*(graph: ModuleGraph): PSym = graph.makeModule(AbsoluteFile"stdin")