summary refs log tree commit diff stats
path: root/compiler/modules.nim
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/modules.nim')
-rw-r--r--compiler/modules.nim79
1 files changed, 41 insertions, 38 deletions
diff --git a/compiler/modules.nim b/compiler/modules.nim
index a2b739efc..2d0267c93 100644
--- a/compiler/modules.nim
+++ b/compiler/modules.nim
@@ -10,19 +10,19 @@
 ## implements the module handling
 
 import
-  ast, astalgo, magicsys, crc, rodread, msgs, cgendata, sigmatch, options, 
-  idents, os, lexer, idgen, passes, syntaxes
+  ast, astalgo, magicsys, securehash, rodread, msgs, cgendata, sigmatch, options,
+  idents, os, lexer, idgen, passes, syntaxes, llstream
 
 type
   TNeedRecompile* = enum Maybe, No, Yes, Probing, Recompiled
-  TCrcStatus* = enum crcNotTaken, crcCached, crcHasChanged, crcNotChanged
+  THashStatus* = enum hashNotTaken, hashCached, hashHasChanged, hashNotChanged
 
   TModuleInMemory* = object
     compiledAt*: float
-    crc*: TCrc32
+    hash*: SecureHash
     deps*: seq[int32] ## XXX: slurped files are currently not tracked
     needsRecompile*: TNeedRecompile
-    crcStatus*: TCrcStatus
+    hashStatus*: THashStatus
 
 var
   gCompiledModules: seq[PSym] = @[]
@@ -34,36 +34,36 @@ proc getModule(fileIdx: int32): PSym =
   if fileIdx >= 0 and fileIdx < gCompiledModules.len:
     result = gCompiledModules[fileIdx]
 
-template crc(x: PSym): expr =
-  gMemCacheData[x.position].crc
+template hash(x: PSym): expr =
+  gMemCacheData[x.position].hash
 
-proc crcChanged(fileIdx: int32): bool =
+proc hashChanged(fileIdx: int32): bool =
   internalAssert fileIdx >= 0 and fileIdx < gMemCacheData.len
-  
+
   template updateStatus =
-    gMemCacheData[fileIdx].crcStatus = if result: crcHasChanged
-                                       else: crcNotChanged
-    # echo "TESTING CRC: ", fileIdx.toFilename, " ", result
-  
-  case gMemCacheData[fileIdx].crcStatus:
-  of crcHasChanged:
+    gMemCacheData[fileIdx].hashStatus = if result: hashHasChanged
+                                       else: hashNotChanged
+    # echo "TESTING Hash: ", fileIdx.toFilename, " ", result
+
+  case gMemCacheData[fileIdx].hashStatus:
+  of hashHasChanged:
     result = true
-  of crcNotChanged:
+  of hashNotChanged:
     result = false
-  of crcCached:
-    let newCrc = crcFromFile(fileIdx.toFilename)
-    result = newCrc != gMemCacheData[fileIdx].crc
-    gMemCacheData[fileIdx].crc = newCrc
+  of hashCached:
+    let newHash = secureHashFile(fileIdx.toFullPath)
+    result = newHash != gMemCacheData[fileIdx].hash
+    gMemCacheData[fileIdx].hash = newHash
     updateStatus()
-  of crcNotTaken:
-    gMemCacheData[fileIdx].crc = crcFromFile(fileIdx.toFilename)
+  of hashNotTaken:
+    gMemCacheData[fileIdx].hash = secureHashFile(fileIdx.toFullPath)
     result = true
     updateStatus()
 
-proc doCRC(fileIdx: int32) =
-  if gMemCacheData[fileIdx].crcStatus == crcNotTaken:
-    # echo "FIRST CRC: ", fileIdx.ToFilename
-    gMemCacheData[fileIdx].crc = crcFromFile(fileIdx.toFilename)
+proc doHash(fileIdx: int32) =
+  if gMemCacheData[fileIdx].hashStatus == hashNotTaken:
+    # echo "FIRST Hash: ", fileIdx.ToFilename
+    gMemCacheData[fileIdx].hash = secureHashFile(fileIdx.toFullPath)
 
 proc addDep(x: PSym, dep: int32) =
   growCache gMemCacheData, dep
@@ -94,9 +94,9 @@ proc checkDepMem(fileIdx: int32): TNeedRecompile =
     return gMemCacheData[fileIdx].needsRecompile
 
   if optForceFullMake in gGlobalOptions or
-     crcChanged(fileIdx):
+     hashChanged(fileIdx):
        markDirty
-  
+
   if gMemCacheData[fileIdx].deps != nil:
     gMemCacheData[fileIdx].needsRecompile = Probing
     for dep in gMemCacheData[fileIdx].deps:
@@ -104,30 +104,30 @@ proc checkDepMem(fileIdx: int32): TNeedRecompile =
       if d in {Yes, Recompiled}:
         # echo fileIdx.toFilename, " depends on ", dep.toFilename, " ", d
         markDirty
-  
+
   gMemCacheData[fileIdx].needsRecompile = No
   return No
 
 proc newModule(fileIdx: int32): PSym =
   # We cannot call ``newSym`` here, because we have to circumvent the ID
-  # mechanism, which we do in order to assign each module a persistent ID. 
+  # mechanism, which we do in order to assign each module a persistent ID.
   new(result)
   result.id = - 1             # for better error checking
   result.kind = skModule
   let filename = fileIdx.toFullPath
   result.name = getIdent(splitFile(filename).name)
-  if result.name.s != "-" and not isNimIdentifier(result.name.s):
+  if not isNimIdentifier(result.name.s):
     rawMessage(errInvalidModuleName, result.name.s)
-  
+
   result.info = newLineInfo(fileIdx, 1, 1)
   result.owner = newSym(skPackage, getIdent(getPackageName(filename)), nil,
                         result.info)
   result.position = fileIdx
-  
+
   growCache gMemCacheData, fileIdx
   growCache gCompiledModules, fileIdx
   gCompiledModules[result.position] = result
-  
+
   incl(result.flags, sfUsed)
   initStrTable(result.tab)
   strTableAdd(result.tab, result) # a module knows itself
@@ -143,16 +143,19 @@ proc compileModule*(fileIdx: int32, flags: TSymFlags): PSym =
     result.flags = result.flags + flags
     if gCmd in {cmdCompileToC, cmdCompileToCpp, cmdCheck, cmdIdeTools}:
       rd = handleSymbolFile(result)
-      if result.id < 0: 
+      if result.id < 0:
         internalError("handleSymbolFile should have set the module\'s ID")
         return
     else:
       result.id = getID()
-    processModule(result, nil, rd)
+    if sfMainModule in flags and gProjectIsStdin:
+      processModule(result, llStreamOpen(stdin), rd)
+    else:
+      processModule(result, nil, rd)
     if optCaasEnabled in gGlobalOptions:
       gMemCacheData[fileIdx].compiledAt = gLastCmdTime
       gMemCacheData[fileIdx].needsRecompile = Recompiled
-      doCRC fileIdx
+      doHash fileIdx
   else:
     if checkDepMem(fileIdx) == Yes:
       result = compileModule(fileIdx, flags)
@@ -171,7 +174,7 @@ proc includeModule*(s: PSym, fileIdx: int32): PNode {.procvar.} =
   if optCaasEnabled in gGlobalOptions:
     growCache gMemCacheData, fileIdx
     addDep(s, fileIdx)
-    doCRC(fileIdx)
+    doHash(fileIdx)
 
 proc `==^`(a, b: string): bool =
   try: