summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-07-07 01:02:12 +0200
committerAndreas Rumpf <rumpf_a@web.de>2016-07-07 01:02:12 +0200
commite9eab32e54815bfad09fb0120aa4b024106f00ba (patch)
tree3b9a9f9e749d1000c74943253e57aee97428c394 /compiler
parent28940ce457e7a7f3e2d1d6cc0223bc0282ce509e (diff)
downloadNim-e9eab32e54815bfad09fb0120aa4b024106f00ba.tar.gz
new language feature: explicit 'import system' statements are allowed
Diffstat (limited to 'compiler')
-rw-r--r--compiler/modules.nim4
-rw-r--r--compiler/sem.nim25
-rw-r--r--compiler/semdata.nim1
3 files changed, 25 insertions, 5 deletions
diff --git a/compiler/modules.nim b/compiler/modules.nim
index 8ac964321..03b9dc9f0 100644
--- a/compiler/modules.nim
+++ b/compiler/modules.nim
@@ -184,8 +184,8 @@ proc importModule*(s: PSym, fileIdx: int32): PSym {.procvar.} =
   # this is called by the semantic checking phase
   result = compileModule(fileIdx, {})
   if optCaasEnabled in gGlobalOptions: addDep(s, fileIdx)
-  if sfSystemModule in result.flags:
-    localError(result.info, errAttemptToRedefine, result.name.s)
+  #if sfSystemModule in result.flags:
+  #  localError(result.info, errAttemptToRedefine, result.name.s)
   # restore the notes for outer module:
   gNotes = if s.owner.id == gMainPackageId: gMainPackageNotes
            else: ForeignPackageNotes
diff --git a/compiler/sem.nim b/compiler/sem.nim
index 6e462dec4..bd7c97fcb 100644
--- a/compiler/sem.nim
+++ b/compiler/sem.nim
@@ -418,9 +418,6 @@ proc myOpen(module: PSym): PPassContext =
   c.importTable.addSym(module) # a module knows itself
   if sfSystemModule in module.flags:
     magicsys.systemModule = module # set global variable!
-  else:
-    c.importTable.addSym magicsys.systemModule # import the "System" identifier
-    importAllSymbols(c, magicsys.systemModule)
   c.topLevelScope = openScope(c)
   # don't be verbose unless the module belongs to the main package:
   if module.owner.id == gMainPackageId:
@@ -434,7 +431,29 @@ proc myOpenCached(module: PSym, rd: PRodReader): PPassContext =
   result = myOpen(module)
   for m in items(rd.methods): methodDef(m, true)
 
+proc isImportSystemStmt(n: PNode): bool =
+  if magicsys.systemModule == nil: return false
+  case n.kind
+  of nkImportStmt:
+    for x in n:
+      let f = checkModuleName(x)
+      if f == magicsys.systemModule.info.fileIndex:
+        return true
+  of nkImportExceptStmt, nkFromStmt:
+    let f = checkModuleName(n[0])
+    if f == magicsys.systemModule.info.fileIndex:
+      return true
+  else: discard
+
 proc semStmtAndGenerateGenerics(c: PContext, n: PNode): PNode =
+  if c.topStmts == 0 and not isImportSystemStmt(n):
+    if sfSystemModule notin c.module.flags and
+        n.kind notin {nkEmpty, nkCommentStmt}:
+      c.importTable.addSym magicsys.systemModule # import the "System" identifier
+      importAllSymbols(c, magicsys.systemModule)
+      inc c.topStmts
+  else:
+    inc c.topStmts
   if sfNoForward in c.module.flags:
     result = semAllTypeSections(c, n)
   else:
diff --git a/compiler/semdata.nim b/compiler/semdata.nim
index 91a1ebf86..52b5dc274 100644
--- a/compiler/semdata.nim
+++ b/compiler/semdata.nim
@@ -99,6 +99,7 @@ type
     unknownIdents*: IntSet     # ids of all unknown identifiers to prevent
                                # naming it multiple times
     generics*: seq[TInstantiationPair] # pending list of instantiated generics to compile
+    topStmts*: int # counts the number of encountered top level statements
     lastGenericIdx*: int      # used for the generics stack
     hloLoopDetector*: int     # used to prevent endless loops in the HLO
     inParallelStmt*: int