This commit is contained in:
araq
2025-11-20 13:25:37 +01:00
parent 15124f05b6
commit 9d7007a2a7
2 changed files with 99 additions and 10 deletions

View File

@@ -1467,6 +1467,8 @@ proc genProcPrototype(m: BModule, sym: PSym) =
m.s[cfsProcHeaders].add(extract(header))
m.s[cfsProcHeaders].finishProcHeaderAsProto()
include inliner
# TODO: figure out how to rename this - it DOES generate a forward declaration
proc genProcNoForward(m: BModule, prc: PSym) =
if lfImportCompilerProc in prc.loc.flags:
@@ -1505,16 +1507,22 @@ proc genProcNoForward(m: BModule, prc: PSym) =
#if prc.loc.k == locNone:
# mangle the inline proc based on the module where it is defined -
# not on the first module that uses it
let m2 = if m.config.symbolFiles != disabledSf: m
else: findPendingModule(m, prc)
fillProcLoc(m2, prc.ast[namePos])
#elif {sfExportc, sfImportc} * prc.flags == {}:
# # reset name to restore consistency in case of hashing collisions:
# echo "resetting ", prc.id, " by ", m.module.name.s
# prc.loc.snippet = nil
# prc.loc.snippet = mangleName(m, prc)
genProcPrototype(m, prc)
genProcAux(m, prc)
if optCompress in m.config.globalOptions:
let prcCopy = copyInlineProc(prc, m.idgen)
fillProcLoc(m, prcCopy.ast[namePos])
genProcPrototype(m, prcCopy)
genProcAux(m, prcCopy)
else:
let m2 = if m.config.symbolFiles != disabledSf: m
else: findPendingModule(m, prc)
fillProcLoc(m2, prc.ast[namePos])
#elif {sfExportc, sfImportc} * prc.flags == {}:
# # reset name to restore consistency in case of hashing collisions:
# echo "resetting ", prc.id, " by ", m.module.name.s
# prc.loc.snippet = nil
# prc.loc.snippet = mangleName(m, prc)
genProcPrototype(m, prc)
genProcAux(m, prc)
elif sfImportc notin prc.flags:
var q = findPendingModule(m, prc)
fillProcLoc(q, prc.ast[namePos])

81
compiler/inliner.nim Normal file
View File

@@ -0,0 +1,81 @@
proc copyInlineProcBody(n: PNode; locals: var Table[int, PSym]; idgen: IdGenerator): PNode =
case n.kind
of nkEmpty..pred(nkSym), succ(nkSym)..nkNilLit:
result = n
of nkSym:
let sym = locals.getOrDefault(n.sym.id)
if sym != nil:
result = newSymNode(sym, n.info)
else:
result = n
of nkLetSection, nkVarSection, nkConstSection:
result = shallowCopy(n)
for i in 0..<n.len:
let it = n[i]
if it.kind == nkCommentStmt:
result[i] = it
elif it.kind == nkIdentDefs:
if it[0].kind == nkSym:
let oldSym = it[0].sym
let newSym = copySym(oldSym, idgen)
locals[oldSym.id] = newSym
result[i] = shallowCopy(it)
result[i][0] = newSymNode(newSym, oldSym.info)
for j in 1..<it.len:
result[i][j] = copyInlineProcBody(it[j], locals, idgen)
else:
result[i] = it
else:
assert it.kind == nkVarTuple
result[i] = shallowCopy(it)
for j in 0..<it.len-2:
assert it[j].kind == nkSym
let oldSym = it[j].sym
let newSym = copySym(oldSym, idgen)
locals[oldSym.id] = newSym
result[i][j] = newSymNode(newSym, oldSym.info)
for j in it.len-2..<it.len:
result[i][j] = copyInlineProcBody(it[j], locals, idgen)
of routineDefs, nkTypeSection, nkTypeOfExpr, nkMixinStmt, nkBindStmt:
result = n
else:
result = shallowCopy(n)
for i in 0..<n.len:
result[i] = copyInlineProcBody(n[i], locals, idgen)
proc copyParams(n: PNode; locals: var Table[int, PSym]; idgen: IdGenerator): PNode =
result = shallowCopy(n)
result[0] = n[0] # return type
for i in 1..<n.len:
let it = n[i]
assert it.kind == nkIdentDefs
assert it[0].kind == nkSym
let oldSym = it[0].sym
let newSym = copySym(oldSym, idgen)
locals[oldSym.id] = newSym
result[i] = shallowCopy(it)
result[i][0] = newSymNode(newSym, oldSym.info)
for j in 1..<it.len:
result[i][j] = copyInlineProcBody(it[j], locals, idgen)
proc copyInlineProc(prc: PSym; idgen: IdGenerator): PSym =
result = copySym(prc, idgen)
var locals = initTable[int, PSym]()
var a = newNodeI(prc.ast.kind, prc.ast.info)
for i in 0..<prc.ast.len:
if i == paramsPos:
a.add copyParams(prc.ast[i], locals, idgen)
elif i == resultPos:
assert prc.ast[i].kind == nkSym
let oldRes = prc.ast[i].sym
let newRes = copySym(oldRes, idgen)
locals[oldRes.id] = newRes
a.add newSymNode(newRes, oldRes.info)
else:
a.add copyInlineProcBody(prc.ast[i], locals, idgen)
result.ast = a