modules.nim 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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, magicsys, msgs, options,
  12. idents, lexer, syntaxes, modulegraphs,
  13. lineinfos, pathutils
  14. import ../dist/checksums/src/checksums/sha1
  15. import std/strtabs
  16. proc resetSystemArtifacts*(g: ModuleGraph) =
  17. magicsys.resetSysTypes(g)
  18. template getModuleIdent(graph: ModuleGraph, filename: AbsoluteFile): PIdent =
  19. getIdent(graph.cache, splitFile(filename).name)
  20. proc partialInitModule*(result: PSym; graph: ModuleGraph; fileIdx: FileIndex; filename: AbsoluteFile) =
  21. let packSym = getPackage(graph, fileIdx)
  22. result.owner = packSym
  23. result.position = int fileIdx
  24. proc newModule*(graph: ModuleGraph; fileIdx: FileIndex): PSym =
  25. let filename = AbsoluteFile toFullPath(graph.config, fileIdx)
  26. # We cannot call ``newSym`` here, because we have to circumvent the ID
  27. # mechanism, which we do in order to assign each module a persistent ID.
  28. result = PSym(kind: skModule, itemId: ItemId(module: int32(fileIdx), item: 0'i32),
  29. name: getModuleIdent(graph, filename),
  30. info: newLineInfo(fileIdx, 1, 1))
  31. if not isNimIdentifier(result.name.s):
  32. rawMessage(graph.config, errGenerated, "invalid module name: '" & result.name.s &
  33. "'; a module name must be a valid Nim identifier.")
  34. partialInitModule(result, graph, fileIdx, filename)
  35. graph.registerModule(result)
  36. proc includeModule*(graph: ModuleGraph; s: PSym, fileIdx: FileIndex): PNode =
  37. result = syntaxes.parseFile(fileIdx, graph.cache, graph.config)
  38. graph.addDep(s, fileIdx)
  39. graph.addIncludeDep(s.position.FileIndex, fileIdx)
  40. let path = toFullPath(graph.config, fileIdx)
  41. graph.cachedFiles[path] = $secureHashFile(path)
  42. proc wantMainModule*(conf: ConfigRef) =
  43. if conf.projectFull.isEmpty:
  44. fatal(conf, gCmdLineInfo, "command expects a filename")
  45. conf.projectMainIdx = fileInfoIdx(conf, addFileExt(conf.projectFull, NimExt))
  46. proc makeModule*(graph: ModuleGraph; filename: AbsoluteFile): PSym =
  47. result = graph.newModule(fileInfoIdx(graph.config, filename))
  48. registerModule(graph, result)
  49. proc makeModule*(graph: ModuleGraph; filename: string): PSym =
  50. result = makeModule(graph, AbsoluteFile filename)
  51. proc makeStdinModule*(graph: ModuleGraph): PSym = graph.makeModule(AbsoluteFile"stdin")