mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-14 15:23:27 +00:00
refactoring: fewer global variables
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
|
||||
@@ -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],
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user