refactoring: make projectMainIdx typesafe

This commit is contained in:
Andreas Rumpf
2018-05-27 22:30:36 +02:00
parent 40ec7be45c
commit 545b1582cd
7 changed files with 17 additions and 21 deletions

View File

@@ -33,22 +33,22 @@ when declared(echo):
template mdbg*: bool {.dirty.} =
when compiles(c.module):
c.module.fileIdx.int32 == c.config.projectMainIdx
c.module.fileIdx == c.config.projectMainIdx
elif compiles(c.c.module):
c.c.module.fileIdx.int32 == c.c.config.projectMainIdx
c.c.module.fileIdx == c.c.config.projectMainIdx
elif compiles(m.c.module):
m.c.module.fileIdx.int32 == m.c.config.projectMainIdx
m.c.module.fileIdx == m.c.config.projectMainIdx
elif compiles(cl.c.module):
cl.c.module.fileIdx.int32 == cl.c.config.projectMainIdx
cl.c.module.fileIdx == cl.c.config.projectMainIdx
elif compiles(p):
when compiles(p.lex):
p.lex.fileIdx.int32 == p.lex.config.projectMainIdx
p.lex.fileIdx == p.lex.config.projectMainIdx
else:
p.module.module.fileIdx.int32 == p.config.projectMainIdx
p.module.module.fileIdx == p.config.projectMainIdx
elif compiles(m.module.fileIdx):
m.module.fileIdx.int32 == m.config.projectMainIdx
m.module.fileIdx == m.config.projectMainIdx
elif compiles(L.fileIdx):
L.fileIdx.int32 == L.config.projectMainIdx
L.fileIdx == L.config.projectMainIdx
else:
error()

View File

@@ -807,7 +807,7 @@ proc writeOutputJson*(d: PDoc, filename, outExt: string,
discard "fixme: error report"
proc commandDoc*(cache: IdentCache, conf: ConfigRef) =
var ast = parseFile(conf.projectMainIdx.FileIndex, newIdentCache(), conf)
var ast = parseFile(conf.projectMainIdx, cache, conf)
if ast == nil: return
var d = newDocumentor(conf.projectFull, cache, conf)
d.hasToc = true
@@ -857,7 +857,7 @@ proc commandRst2TeX*(cache: IdentCache, conf: ConfigRef) =
commandRstAux(cache, conf, conf.projectFull, TexExt)
proc commandJson*(cache: IdentCache, conf: ConfigRef) =
var ast = parseFile(conf.projectMainIdx.FileIndex, newIdentCache(), conf)
var ast = parseFile(conf.projectMainIdx, cache, conf)
if ast == nil: return
var d = newDocumentor(conf.projectFull, cache, conf)
d.hasToc = true
@@ -874,7 +874,7 @@ proc commandJson*(cache: IdentCache, conf: ConfigRef) =
rawMessage(conf, errCannotOpenFile, filename)
proc commandTags*(cache: IdentCache, conf: ConfigRef) =
var ast = parseFile(conf.projectMainIdx.FileIndex, newIdentCache(), conf)
var ast = parseFile(conf.projectMainIdx, cache, conf)
if ast == nil: return
var d = newDocumentor(conf.projectFull, cache, conf)
d.hasToc = true

View File

@@ -261,7 +261,7 @@ proc mainCommand*(graph: ModuleGraph; cache: IdentCache) =
of "parse":
conf.cmd = cmdParse
wantMainModule(conf)
discard parseFile(FileIndex conf.projectMainIdx, cache, conf)
discard parseFile(conf.projectMainIdx, cache, conf)
of "scan":
conf.cmd = cmdScan
wantMainModule(conf)

View File

@@ -124,7 +124,7 @@ proc compileSystemModule*(graph: ModuleGraph; cache: IdentCache) =
proc wantMainModule*(conf: ConfigRef) =
if conf.projectFull.len == 0:
fatal(conf, newLineInfo(conf, "command line", 1, 1), errGenerated, "command expects a filename")
conf.projectMainIdx = int32 fileInfoIdx(conf, addFileExt(conf.projectFull, NimExt))
conf.projectMainIdx = fileInfoIdx(conf, addFileExt(conf.projectFull, NimExt))
passes.gIncludeFile = includeModule
passes.gImportModule = importModule
@@ -134,7 +134,7 @@ proc compileProject*(graph: ModuleGraph; cache: IdentCache;
let conf = graph.config
wantMainModule(conf)
let systemFileIdx = fileInfoIdx(conf, conf.libpath / "system.nim")
let projectFile = if projectFileIdx == InvalidFileIDX: FileIndex(conf.projectMainIdx) else: projectFileIdx
let projectFile = if projectFileIdx == InvalidFileIDX: conf.projectMainIdx else: projectFileIdx
graph.importStack.add projectFile
if projectFile == systemFileIdx:
discard graph.compileModule(projectFile, cache, {sfMainModule, sfSystemModule})

View File

@@ -79,7 +79,7 @@ proc handleCmdLine(cache: IdentCache; conf: ConfigRef) =
conf.projectName = p.name
else:
conf.projectPath = canonicalizePath(conf, getCurrentDir())
loadConfigs(DefaultConfig, conf) # load all config files
loadConfigs(DefaultConfig, cache, conf) # load all config files
let scriptFile = conf.projectFull.changeFileExt("nims")
if fileExists(scriptFile):
runNimScript(cache, scriptFile, freshDefines=false, conf)

View File

@@ -253,7 +253,3 @@ proc loadConfigs*(cfg: string; cache: IdentCache; conf: ConfigRef) =
if not fileExists(projectConfig):
projectConfig = changeFileExt(conf.projectFull, "nim.cfg")
readConfigFile(projectConfig, cache, conf)
proc loadConfigs*(cfg: string; conf: ConfigRef) =
# for backwards compatibility only.
loadConfigs(cfg, newIdentCache(), conf)

View File

@@ -204,7 +204,7 @@ type
projectPath*: string # holds a path like /home/alice/projects/nim/compiler/
projectFull*: string # projectPath/projectName
projectIsStdin*: bool # whether we're compiling from stdin
projectMainIdx*: int32 # the canonical path id of the main module
projectMainIdx*: FileIndex # the canonical path id of the main module
command*: string # the main command (e.g. cc, check, scan, etc)
commandArgs*: seq[string] # any arguments after the main command
keepComments*: bool # whether the parser needs to keep comments
@@ -278,7 +278,7 @@ proc newConfigRef*(): ConfigRef =
projectPath: "", # holds a path like /home/alice/projects/nim/compiler/
projectFull: "", # projectPath/projectName
projectIsStdin: false, # whether we're compiling from stdin
projectMainIdx: 0'i32, # the canonical path id of the main module
projectMainIdx: FileIndex(0'i32), # the canonical path id of the main module
command: "", # the main command (e.g. cc, check, scan, etc)
commandArgs: @[], # any arguments after the main command
keepComments: true, # whether the parser needs to keep comments