modules.nim 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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
  14. proc resetSystemArtifacts*(g: ModuleGraph) =
  15. magicsys.resetSysTypes(g)
  16. proc newModule(graph: ModuleGraph; fileIdx: FileIndex): PSym =
  17. # We cannot call ``newSym`` here, because we have to circumvent the ID
  18. # mechanism, which we do in order to assign each module a persistent ID.
  19. new(result)
  20. result.id = -1 # for better error checking
  21. result.kind = skModule
  22. let filename = toFullPath(graph.config, fileIdx)
  23. result.name = getIdent(graph.cache, splitFile(filename).name)
  24. if not isNimIdentifier(result.name.s):
  25. rawMessage(graph.config, errGenerated, "invalid module name: " & result.name.s)
  26. result.info = newLineInfo(fileIdx, 1, 1)
  27. let
  28. pck = getPackageName(graph.config, filename)
  29. pck2 = if pck.len > 0: pck else: "unknown"
  30. pack = getIdent(graph.cache, pck2)
  31. var packSym = graph.packageSyms.strTableGet(pack)
  32. if packSym == nil:
  33. packSym = newSym(skPackage, getIdent(graph.cache, pck2), nil, result.info)
  34. initStrTable(packSym.tab)
  35. graph.packageSyms.strTableAdd(packSym)
  36. result.owner = packSym
  37. result.position = int fileIdx
  38. if int(fileIdx) >= graph.modules.len:
  39. setLen(graph.modules, int(fileIdx) + 1)
  40. #growCache graph.modules, int fileIdx
  41. graph.modules[result.position] = result
  42. incl(result.flags, sfUsed)
  43. initStrTable(result.tab)
  44. strTableAdd(result.tab, result) # a module knows itself
  45. let existing = strTableGet(packSym.tab, result.name)
  46. if existing != nil and existing.info.fileIndex != result.info.fileIndex:
  47. localError(graph.config, result.info,
  48. "module names need to be unique per Nimble package; module clashes with " &
  49. toFullPath(graph.config, existing.info.fileIndex))
  50. # strTableIncl() for error corrections:
  51. discard strTableIncl(packSym.tab, result)
  52. proc compileModule*(graph: ModuleGraph; fileIdx: FileIndex; flags: TSymFlags): PSym =
  53. result = graph.getModule(fileIdx)
  54. if result == nil:
  55. result = newModule(graph, fileIdx)
  56. result.flags = result.flags + flags
  57. if sfMainModule in result.flags:
  58. graph.config.mainPackageId = result.owner.id
  59. result.id = getModuleId(graph, fileIdx, toFullPath(graph.config, fileIdx))
  60. discard processModule(graph, result,
  61. if sfMainModule in flags and graph.config.projectIsStdin: stdin.llStreamOpen else: nil)
  62. elif graph.isDirty(result):
  63. result.flags.excl sfDirty
  64. # reset module fields:
  65. initStrTable(result.tab)
  66. result.ast = nil
  67. discard processModule(graph, result,
  68. if sfMainModule in flags and graph.config.projectIsStdin: stdin.llStreamOpen else: nil)
  69. graph.markClientsDirty(fileIdx)
  70. proc importModule*(graph: ModuleGraph; s: PSym, fileIdx: FileIndex): PSym {.procvar.} =
  71. # this is called by the semantic checking phase
  72. assert graph.config != nil
  73. result = compileModule(graph, fileIdx, {})
  74. graph.addDep(s, fileIdx)
  75. #if sfSystemModule in result.flags:
  76. # localError(result.info, errAttemptToRedefine, result.name.s)
  77. # restore the notes for outer module:
  78. graph.config.notes =
  79. if s.owner.id == graph.config.mainPackageId: graph.config.mainPackageNotes
  80. else: graph.config.foreignPackageNotes
  81. proc includeModule*(graph: ModuleGraph; s: PSym, fileIdx: FileIndex): PNode {.procvar.} =
  82. result = syntaxes.parseFile(fileIdx, graph.cache, graph.config)
  83. graph.addDep(s, fileIdx)
  84. graph.addIncludeDep(s.position.FileIndex, fileIdx)
  85. proc connectCallbacks*(graph: ModuleGraph) =
  86. graph.includeFileCallback = includeModule
  87. graph.importModuleCallback = importModule
  88. proc compileSystemModule*(graph: ModuleGraph) =
  89. if graph.systemModule == nil:
  90. connectCallbacks(graph)
  91. graph.config.m.systemFileIdx = fileInfoIdx(graph.config,
  92. graph.config.libpath / RelativeFile"system.nim")
  93. discard graph.compileModule(graph.config.m.systemFileIdx, {sfSystemModule})
  94. proc wantMainModule*(conf: ConfigRef) =
  95. if conf.projectFull.isEmpty:
  96. fatal(conf, newLineInfo(conf, AbsoluteFile"command line", 1, 1), errGenerated,
  97. "command expects a filename")
  98. conf.projectMainIdx = fileInfoIdx(conf, addFileExt(conf.projectFull, NimExt))
  99. proc compileProject*(graph: ModuleGraph; projectFileIdx = InvalidFileIDX) =
  100. connectCallbacks(graph)
  101. let conf = graph.config
  102. wantMainModule(conf)
  103. let systemFileIdx = fileInfoIdx(conf, conf.libpath / RelativeFile"system.nim")
  104. let projectFile = if projectFileIdx == InvalidFileIDX: conf.projectMainIdx else: projectFileIdx
  105. graph.importStack.add projectFile
  106. if projectFile == systemFileIdx:
  107. discard graph.compileModule(projectFile, {sfMainModule, sfSystemModule})
  108. else:
  109. graph.compileSystemModule()
  110. discard graph.compileModule(projectFile, {sfMainModule})
  111. proc makeModule*(graph: ModuleGraph; filename: AbsoluteFile): PSym =
  112. result = graph.newModule(fileInfoIdx(graph.config, filename))
  113. result.id = getID()
  114. proc makeModule*(graph: ModuleGraph; filename: string): PSym =
  115. result = makeModule(graph, AbsoluteFile filename)
  116. proc makeStdinModule*(graph: ModuleGraph): PSym = graph.makeModule(AbsoluteFile"stdin")