Fix compiler build with -d:nimDebugReorder (#20888)

* add missing import for -d:debugReorder

* rename compile-time symbol to match best practices

Fix #20887.
This commit is contained in:
Kyle Coffey 2022-11-22 08:51:17 -06:00 • committed by GitHub
commit f4553af343
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,9 @@ import
when defined(nimPreviewSlimSystem): when defined(nimPreviewSlimSystem):
import std/assertions import std/assertions
when defined(nimDebugReorder):
import std/tables
type type
DepN = ref object DepN = ref object
pnode: PNode pnode: PNode
@ -14,11 +17,11 @@ type
onStack: bool onStack: bool
kids: seq[DepN] kids: seq[DepN]
hAQ, hIS, hB, hCmd: int hAQ, hIS, hB, hCmd: int
when defined(debugReorder): when defined(nimDebugReorder):
expls: seq[string] expls: seq[string]
DepG = seq[DepN] DepG = seq[DepN]
when defined(debugReorder): when defined(nimDebugReorder):
var idNames = newTable[int, string]() var idNames = newTable[int, string]()
proc newDepN(id: int, pnode: PNode): DepN = proc newDepN(id: int, pnode: PNode): DepN =
@ -33,7 +36,7 @@ proc newDepN(id: int, pnode: PNode): DepN =
result.hIS = -1 result.hIS = -1
result.hB = -1 result.hB = -1
result.hCmd = -1 result.hCmd = -1
when defined(debugReorder): when defined(nimDebugReorder):
result.expls = @[] result.expls = @[]
proc accQuoted(cache: IdentCache; n: PNode): PIdent = proc accQuoted(cache: IdentCache; n: PNode): PIdent =
@ -49,16 +52,16 @@ proc addDecl(cache: IdentCache; n: PNode; declares: var IntSet) =
of nkPragmaExpr: addDecl(cache, n[0], declares) of nkPragmaExpr: addDecl(cache, n[0], declares)
of nkIdent: of nkIdent:
declares.incl n.ident.id declares.incl n.ident.id
when defined(debugReorder): when defined(nimDebugReorder):
idNames[n.ident.id] = n.ident.s idNames[n.ident.id] = n.ident.s
of nkSym: of nkSym:
declares.incl n.sym.name.id declares.incl n.sym.name.id
when defined(debugReorder): when defined(nimDebugReorder):
idNames[n.sym.name.id] = n.sym.name.s idNames[n.sym.name.id] = n.sym.name.s
of nkAccQuoted: of nkAccQuoted:
let a = accQuoted(cache, n) let a = accQuoted(cache, n)
declares.incl a.id declares.incl a.id
when defined(debugReorder): when defined(nimDebugReorder):
idNames[a.id] = a.s idNames[a.id] = a.s
of nkEnumFieldDef: of nkEnumFieldDef:
addDecl(cache, n[0], declares) addDecl(cache, n[0], declares)
@ -193,7 +196,7 @@ proc mergeSections(conf: ConfigRef; comps: seq[seq[DepN]], res: PNode) =
# consecutive type and const sections # consecutive type and const sections
var wmsg = "Circular dependency detected. `codeReordering` pragma may not be able to" & var wmsg = "Circular dependency detected. `codeReordering` pragma may not be able to" &
" reorder some nodes properly" " reorder some nodes properly"
when defined(debugReorder): when defined(nimDebugReorder):
wmsg &= ":\n" wmsg &= ":\n"
for i in 0..<cs.len-1: for i in 0..<cs.len-1:
for j in i..<cs.len: for j in i..<cs.len:
@ -332,13 +335,13 @@ proc buildGraph(n: PNode, deps: seq[(IntSet, IntSet)]): DepG =
if j < i and nj.hasCommand and niHasCmd: if j < i and nj.hasCommand and niHasCmd:
# Preserve order for commands and calls # Preserve order for commands and calls
ni.kids.add nj ni.kids.add nj
when defined(debugReorder): when defined(nimDebugReorder):
ni.expls.add "both have commands and one comes after the other" ni.expls.add "both have commands and one comes after the other"
elif j < i and nj.hasImportStmt: elif j < i and nj.hasImportStmt:
# Every node that comes after an import statement must # Every node that comes after an import statement must
# depend on that import # depend on that import
ni.kids.add nj ni.kids.add nj
when defined(debugReorder): when defined(nimDebugReorder):
ni.expls.add "parent is, or contains, an import statement and child comes after it" ni.expls.add "parent is, or contains, an import statement and child comes after it"
elif j < i and niHasBody and nj.hasAccQuotedDef: elif j < i and niHasBody and nj.hasAccQuotedDef:
# Every function, macro, template... with a body depends # Every function, macro, template... with a body depends
@ -346,13 +349,13 @@ proc buildGraph(n: PNode, deps: seq[(IntSet, IntSet)]): DepG =
# That's because it is hard to detect the use of functions # That's because it is hard to detect the use of functions
# like "[]=", "[]", "or" ... in their bodies. # like "[]=", "[]", "or" ... in their bodies.
ni.kids.add nj ni.kids.add nj
when defined(debugReorder): when defined(nimDebugReorder):
ni.expls.add "one declares a quoted identifier and the other has a body and comes after it" 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 elif j < i and niHasBody and not nj.hasBody and
intersects(deps[i][0], declares): intersects(deps[i][0], declares):
# Keep function declaration before function definition # Keep function declaration before function definition
ni.kids.add nj ni.kids.add nj
when defined(debugReorder): when defined(nimDebugReorder):
for dep in deps[i][0]: for dep in deps[i][0]:
if dep in declares: if dep in declares:
ni.expls.add "one declares \"" & idNames[dep] & "\" and the other defines it" ni.expls.add "one declares \"" & idNames[dep] & "\" and the other defines it"
@ -360,7 +363,7 @@ proc buildGraph(n: PNode, deps: seq[(IntSet, IntSet)]): DepG =
for d in declares: for d in declares:
if uses.contains(d): if uses.contains(d):
ni.kids.add nj ni.kids.add nj
when defined(debugReorder): when defined(nimDebugReorder):
ni.expls.add "one declares \"" & idNames[d] & "\" and the other uses it" ni.expls.add "one declares \"" & idNames[d] & "\" and the other uses it"
proc strongConnect(v: var DepN, idx: var int, s: var seq[DepN], proc strongConnect(v: var DepN, idx: var int, s: var seq[DepN],