modules.nim 2.3 KB

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