summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2018-05-27 19:19:12 +0200
committerAndreas Rumpf <rumpf_a@web.de>2018-05-27 19:19:12 +0200
commita325692fb29e07911adc5c95aa0c38018602ea21 (patch)
treea81165e4aaca4794ed76fb4dc3076e1825c896c5 /compiler
parent18a3833d6085ae4edf28433ba12a27a4be3ae3b1 (diff)
downloadNim-a325692fb29e07911adc5c95aa0c38018602ea21.tar.gz
refactoring: fewer global variables
Diffstat (limited to 'compiler')
-rw-r--r--compiler/plugins/active.nim13
-rw-r--r--compiler/plugins/itersgen.nim6
-rw-r--r--compiler/plugins/locals.nim (renamed from compiler/plugins/locals/locals.nim)6
-rw-r--r--compiler/pluginsupport.nim28
-rw-r--r--compiler/reorder.nim24
-rw-r--r--compiler/semmagic.nim2
6 files changed, 36 insertions, 43 deletions
diff --git a/compiler/plugins/active.nim b/compiler/plugins/active.nim
index 5da623e49..4e82e58cb 100644
--- a/compiler/plugins/active.nim
+++ b/compiler/plugins/active.nim
@@ -10,4 +10,15 @@
 ## Include file that imports all plugins that are active.
 
 import
-  locals / locals, itersgen
+  compiler / [pluginsupport, idents, ast], locals, itersgen
+
+const
+  plugins: array[2, Plugin] = [
+    ("stdlib", "system", "iterToProc", iterToProcImpl),
+    ("stdlib", "system", "locals", semLocals)
+  ]
+
+proc getPlugin*(ic: IdentCache; fn: PSym): Transformation =
+  for p in plugins:
+    if pluginMatches(ic, p, fn): return p.t
+  return nil
diff --git a/compiler/plugins/itersgen.nim b/compiler/plugins/itersgen.nim
index 7af97904e..847d13ccf 100644
--- a/compiler/plugins/itersgen.nim
+++ b/compiler/plugins/itersgen.nim
@@ -9,11 +9,11 @@
 
 ## Plugin to transform an inline iterator into a data structure.
 
-import ".." / [pluginsupport, ast, astalgo,
+import ".." / [ast, astalgo,
   magicsys, lookups, semdata,
   lambdalifting, rodread, msgs]
 
-proc iterToProcImpl(c: PContext, n: PNode): PNode =
+proc iterToProcImpl*(c: PContext, n: PNode): PNode =
   result = newNodeI(nkStmtList, n.info)
   let iter = n[1]
   if iter.kind != nkSym or iter.sym.kind != skIterator:
@@ -46,5 +46,3 @@ proc iterToProcImpl(c: PContext, n: PNode): PNode =
 
   prc.ast.add iter.sym.ast.sons[resultPos]
   addInterfaceDecl(c, prc)
-
-registerPlugin("stdlib", "system", "iterToProc", iterToProcImpl)
diff --git a/compiler/plugins/locals/locals.nim b/compiler/plugins/locals.nim
index ff7f3be58..0048ff985 100644
--- a/compiler/plugins/locals/locals.nim
+++ b/compiler/plugins/locals.nim
@@ -9,10 +9,10 @@
 
 ## The builtin 'system.locals' implemented as a plugin.
 
-import "../../" / [pluginsupport, ast, astalgo,
+import ".." / [pluginsupport, ast, astalgo,
   magicsys, lookups, semdata, lowerings]
 
-proc semLocals(c: PContext, n: PNode): PNode =
+proc semLocals*(c: PContext, n: PNode): PNode =
   var counter = 0
   var tupleType = newTypeS(tyTuple, c)
   result = newNodeIT(nkPar, n.info, tupleType)
@@ -39,5 +39,3 @@ proc semLocals(c: PContext, n: PNode): PNode =
         var a = newSymNode(it, result.info)
         if it.typ.skipTypes({tyGenericInst}).kind == tyVar: a = newDeref(a)
         result.add(a)
-
-registerPlugin("stdlib", "system", "locals", semLocals)
diff --git a/compiler/pluginsupport.nim b/compiler/pluginsupport.nim
index f67942c97..a44436f11 100644
--- a/compiler/pluginsupport.nim
+++ b/compiler/pluginsupport.nim
@@ -8,40 +8,26 @@
 #
 
 ## Plugin support for the Nim compiler. Right now plugins
-## need to be built with the compiler only: plugins using 
+## need to be built with the compiler only: plugins using
 ## DLLs or the FFI will not work.
 
 import ast, semdata, idents
 
 type
   Transformation* = proc (c: PContext; n: PNode): PNode {.nimcall.}
-  Plugin = ref object
-    fn, module, package: PIdent
+  Plugin* = tuple
+    package, module, fn: string
     t: Transformation
-    next: Plugin
 
-proc pluginMatches(p: Plugin; s: PSym): bool =
-  if s.name.id != p.fn.id:
+proc pluginMatches*(ic: IdentCache; p: Plugin; s: PSym): bool =
+  if s.name.id != ic.getIdent(p.fn).id:
     return false
   let module = s.skipGenericOwner
   if module == nil or module.kind != skModule or
-      module.name.id != p.module.id:
+      module.name.id != ic.getIdent(p.module).id:
     return false
   let package = module.owner
   if package == nil or package.kind != skPackage or
-      package.name.id != p.package.id:
+      package.name.id != ic.getIdent(p.package).id:
     return false
   return true
-
-var head: Plugin
-
-proc getPlugin*(fn: PSym): Transformation =
-  var it = head
-  while it != nil:
-    if pluginMatches(it, fn): return it.t
-    it = it.next
-
-proc registerPlugin*(package, module, fn: string; t: Transformation) =
-  let oldHead = head
-  head = Plugin(fn: getIdent(fn), module: getIdent(module),
-                 package: getIdent(package), t: t, next: oldHead)
diff --git a/compiler/reorder.nim b/compiler/reorder.nim
index 932df9bca..bf5c3b4a9 100644
--- a/compiler/reorder.nim
+++ b/compiler/reorder.nim
@@ -11,11 +11,11 @@ type
     onStack: bool
     kids: seq[DepN]
     hAQ, hIS, hB, hCmd: int
-    when not defined(release):
+    when defined(debugReorder):
       expls: seq[string]
   DepG = seq[DepN]
 
-when not defined(release):
+when defined(debugReorder):
   var idNames = newTable[int, string]()
 
 proc newDepN(id: int, pnode: PNode): DepN =
@@ -30,7 +30,7 @@ proc newDepN(id: int, pnode: PNode): DepN =
   result.hIS = -1
   result.hB = -1
   result.hCmd = -1
-  when not defined(release):
+  when defined(debugReorder):
     result.expls = @[]
 
 proc accQuoted(n: PNode): PIdent =
@@ -49,16 +49,16 @@ proc addDecl(n: PNode; declares: var IntSet) =
   of nkPragmaExpr: addDecl(n[0], declares)
   of nkIdent:
     declares.incl n.ident.id
-    when not defined(release):
+    when defined(debugReorder):
       idNames[n.ident.id] = n.ident.s
   of nkSym:
     declares.incl n.sym.name.id
-    when not defined(release):
+    when defined(debugReorder):
       idNames[n.sym.name.id] = n.sym.name.s
   of nkAccQuoted:
     let a = accQuoted(n)
     declares.incl a.id
-    when not defined(release):
+    when defined(debugReorder):
       idNames[a.id] = a.s
   of nkEnumFieldDef:
     addDecl(n[0], declares)
@@ -216,7 +216,7 @@ proc mergeSections(conf: ConfigRef; comps: seq[seq[DepN]], res: PNode) =
           # consecutive type and const sections
           var wmsg = "Circular dependency detected. reorder pragma may not be able to" &
             " reorder some nodes properely"
-          when not defined(release):
+          when defined(debugReorder):
             wmsg &= ":\n"
             for i in 0..<cs.len-1:
                 for j in i..<cs.len:
@@ -355,13 +355,13 @@ proc buildGraph(n: PNode, deps: seq[(IntSet, IntSet)]): DepG =
       if j < i and nj.hasCommand and niHasCmd:
         # Preserve order for commands and calls
         ni.kids.add nj
-        when not defined(release):
+        when defined(debugReorder):
           ni.expls.add "both have commands and one comes after the other"
       elif j < i and nj.hasImportStmt:
         # Every node that comes after an import statement must
         # depend on that import
         ni.kids.add nj
-        when not defined(release):
+        when defined(debugReorder):
           ni.expls.add "parent is, or contains, an import statement and child comes after it"
       elif j < i and niHasBody and nj.hasAccQuotedDef:
         # Every function, macro, template... with a body depends
@@ -369,13 +369,13 @@ proc buildGraph(n: PNode, deps: seq[(IntSet, IntSet)]): DepG =
         # That's because it is hard to detect the use of functions
         # like "[]=", "[]", "or" ... in their bodies.
         ni.kids.add nj
-        when not defined(release):
+        when defined(debugReorder):
           ni.expls.add "one declares a quoted identifier and the other has a body and comes after it"
       elif j < i and niHasBody and not nj.hasBody and
         intersects(deps[i][0], declares):
           # Keep function declaration before function definition
           ni.kids.add nj
-          when not defined(release):
+          when defined(debugReorder):
             for dep in deps[i][0]:
               if dep in declares:
                 ni.expls.add "one declares \"" & idNames[dep] & "\" and the other defines it"
@@ -383,7 +383,7 @@ proc buildGraph(n: PNode, deps: seq[(IntSet, IntSet)]): DepG =
         for d in declares:
           if uses.contains(d):
             ni.kids.add nj
-            when not defined(release):
+            when defined(debugReorder):
               ni.expls.add "one declares \"" & idNames[d] & "\" and the other uses it"
 
 proc strongConnect(v: var DepN, idx: var int, s: var seq[DepN],
diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim
index bd1cd5a7f..b2dbe3d31 100644
--- a/compiler/semmagic.nim
+++ b/compiler/semmagic.nim
@@ -279,7 +279,7 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
   of mRoof:
     localError(c.config, n.info, "builtin roof operator is not supported anymore")
   of mPlugin:
-    let plugin = getPlugin(n[0].sym)
+    let plugin = getPlugin(c.cache, n[0].sym)
     if plugin.isNil:
       localError(c.config, n.info, "cannot find plugin " & n[0].sym.name.s)
       result = n