the remaining passes all compile again

This commit is contained in:
Andreas Rumpf 2018-05-13 00:45:52 +02:00
commit 8ca5b71c6a
8 changed files with 129 additions and 117 deletions

View file

@ -181,6 +181,7 @@ const
const const
errXMustBeCompileTime* = "'$1' can only be used in compile-time context" errXMustBeCompileTime* = "'$1' can only be used in compile-time context"
errArgsNeedRunOption* = "arguments can only be given if the '--run' option is selected"
#[ #[
errStringLiteralExpected: "string literal expected", errStringLiteralExpected: "string literal expected",
@ -210,7 +211,7 @@ errUnknownCcompiler: "unknown C compiler: '$1'",
errOnOrOffExpectedButXFound: "'on' or 'off' expected, but '$1' found", errOnOrOffExpectedButXFound: "'on' or 'off' expected, but '$1' found",
errOnOffOrListExpectedButXFound: "'on', 'off' or 'list' expected, but '$1' found", errOnOffOrListExpectedButXFound: "'on', 'off' or 'list' expected, but '$1' found",
errGenOutExpectedButXFound: "'c', 'c++' or 'yaml' expected, but '$1' found", errGenOutExpectedButXFound: "'c', 'c++' or 'yaml' expected, but '$1' found",
errArgsNeedRunOption: "arguments can only be given if the '--run' option is selected", ,
errInvalidMultipleAsgn: "multiple assignment is not allowed", errInvalidMultipleAsgn: "multiple assignment is not allowed",
errColonOrEqualsExpected: "':' or '=' expected, but found '$1'", errColonOrEqualsExpected: "':' or '=' expected, but found '$1'",
errUndeclaredField: "undeclared field: '$1'", errUndeclaredField: "undeclared field: '$1'",

View file

@ -19,6 +19,7 @@ proc generateDot*(project: string)
type type
TGen = object of TPassContext TGen = object of TPassContext
module*: PSym module*: PSym
config: ConfigRef
PGen = ref TGen PGen = ref TGen
var gDotGraph: Rope # the generated DOT file; we need a global variable var gDotGraph: Rope # the generated DOT file; we need a global variable
@ -33,10 +34,10 @@ proc addDotDependency(c: PPassContext, n: PNode): PNode =
case n.kind case n.kind
of nkImportStmt: of nkImportStmt:
for i in countup(0, sonsLen(n) - 1): for i in countup(0, sonsLen(n) - 1):
var imported = getModuleName(n.sons[i]) var imported = getModuleName(g.config, n.sons[i])
addDependencyAux(g.module.name.s, imported) addDependencyAux(g.module.name.s, imported)
of nkFromStmt, nkImportExceptStmt: of nkFromStmt, nkImportExceptStmt:
var imported = getModuleName(n.sons[0]) var imported = getModuleName(g.config, n.sons[0])
addDependencyAux(g.module.name.s, imported) addDependencyAux(g.module.name.s, imported)
of nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr: of nkStmtList, nkBlockStmt, nkStmtListExpr, nkBlockExpr:
for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i]) for i in countup(0, sonsLen(n) - 1): discard addDotDependency(c, n.sons[i])
@ -52,6 +53,7 @@ proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
var g: PGen var g: PGen
new(g) new(g)
g.module = module g.module = module
g.config = graph.config
result = g result = g
const gendependPass* = makePass(open = myOpen, process = addDotDependency) const gendependPass* = makePass(open = myOpen, process = addDotDependency)

View file

@ -55,7 +55,7 @@ proc myOpen(graph: ModuleGraph; module: PSym; cache: IdentCache): PPassContext =
var g: PGen var g: PGen
new(g) new(g)
g.module = module g.module = module
var d = newDocumentor(module.filename, options.gConfigVars) var d = newDocumentor(module.filename, graph.config)
d.hasToc = true d.hasToc = true
g.doc = d g.doc = d
result = g result = g

View file

@ -32,13 +32,15 @@ import
ast, astalgo, strutils, hashes, trees, platform, magicsys, extccomp, options, ast, astalgo, strutils, hashes, trees, platform, magicsys, extccomp, options,
nversion, nimsets, msgs, std / sha1, bitsets, idents, types, os, tables, nversion, nimsets, msgs, std / sha1, bitsets, idents, types, os, tables,
times, ropes, math, passes, ccgutils, wordrecg, renderer, rodread, rodutils, times, ropes, math, passes, ccgutils, wordrecg, renderer, rodread, rodutils,
intsets, cgmeth, lowerings, sighashes intsets, cgmeth, lowerings, sighashes, configuration
from modulegraphs import ModuleGraph from modulegraphs import ModuleGraph
type type
TJSGen = object of TPassContext TJSGen = object of TPassContext
module: PSym module: PSym
graph: ModuleGraph
config: ConfigRef
sigConflicts: CountTable[SigHash] sigConflicts: CountTable[SigHash]
BModule = ref TJSGen BModule = ref TJSGen
@ -94,14 +96,14 @@ type
up: PProc # up the call chain; required for closure support up: PProc # up the call chain; required for closure support
declaredGlobals: IntSet declaredGlobals: IntSet
var indent = "\t".rope template config*(p: PProc): ConfigRef = p.module.config
proc indentLine(p: PProc, r: Rope): Rope = proc indentLine(p: PProc, r: Rope): Rope =
result = r result = r
var p = p var p = p
while true: while true:
for i in countup(0, p.blocks.len - 1 + p.extraIndent): for i in countup(0, p.blocks.len - 1 + p.extraIndent):
prepend(result, indent) prepend(result, "\t".rope)
if p.up == nil or p.up.prc != p.prc.owner: if p.up == nil or p.up.prc != p.prc.owner:
break break
p = p.up p = p.up
@ -194,7 +196,7 @@ proc mapType(typ: PType): TJSTypeKind =
else: result = etyNone else: result = etyNone
of tyProc: result = etyProc of tyProc: result = etyProc
of tyCString: result = etyString of tyCString: result = etyString
of tyUnused, tyOptAsRef: internalError("mapType") of tyUnused, tyOptAsRef: doAssert(false, "mapType")
proc mapType(p: PProc; typ: PType): TJSTypeKind = proc mapType(p: PProc; typ: PType): TJSTypeKind =
result = mapType(typ) result = mapType(typ)
@ -286,18 +288,17 @@ proc genConstant(p: PProc, c: PSym)
proc useMagic(p: PProc, name: string) = proc useMagic(p: PProc, name: string) =
if name.len == 0: return if name.len == 0: return
var s = magicsys.getCompilerProc(name) var s = magicsys.getCompilerProc(p.module.graph, name)
if s != nil: if s != nil:
internalAssert s.kind in {skProc, skFunc, skMethod, skConverter} internalAssert p.config, s.kind in {skProc, skFunc, skMethod, skConverter}
if not p.g.generatedSyms.containsOrIncl(s.id): if not p.g.generatedSyms.containsOrIncl(s.id):
let code = genProc(p, s) let code = genProc(p, s)
add(p.g.constants, code) add(p.g.constants, code)
else: else:
# we used to exclude the system module from this check, but for DLL if p.prc != nil:
# generation support this sloppyness leads to hard to detect bugs, so globalError(p.config, p.prc.info, "system module needs: " & name)
# we're picky here for the system module too: else:
if p.prc != nil: globalError(p.prc.info, errSystemNeeds, name) rawMessage(p.config, errGenerated, "system module needs: " & name)
else: rawMessage(errSystemNeeds, name)
proc isSimpleExpr(p: PProc; n: PNode): bool = proc isSimpleExpr(p: PProc; n: PNode): bool =
# calls all the way down --> can stay expression based # calls all the way down --> can stay expression based
@ -547,7 +548,7 @@ proc genLineDir(p: PProc, n: PNode) =
proc genWhileStmt(p: PProc, n: PNode) = proc genWhileStmt(p: PProc, n: PNode) =
var var
cond: TCompRes cond: TCompRes
internalAssert isEmptyType(n.typ) internalAssert p.config, isEmptyType(n.typ)
genLineDir(p, n) genLineDir(p, n)
inc(p.unique) inc(p.unique)
var length = len(p.blocks) var length = len(p.blocks)
@ -634,7 +635,7 @@ proc genTry(p: PProc, n: PNode, r: var TCompRes) =
useMagic(p, "isObj") useMagic(p, "isObj")
for j in countup(0, blen - 2): for j in countup(0, blen - 2):
if n.sons[i].sons[j].kind != nkType: if n.sons[i].sons[j].kind != nkType:
internalError(n.info, "genTryStmt") internalError(p.config, n.info, "genTryStmt")
if orExpr != nil: add(orExpr, "||") if orExpr != nil: add(orExpr, "||")
addf(orExpr, "isObj($2lastJSError.m_type, $1)", addf(orExpr, "isObj($2lastJSError.m_type, $1)",
[genTypeInfo(p, n.sons[i].sons[j].typ), dollar]) [genTypeInfo(p, n.sons[i].sons[j].typ), dollar])
@ -648,7 +649,7 @@ proc genTry(p: PProc, n: PNode, r: var TCompRes) =
if not generalCatchBranchExists: if not generalCatchBranchExists:
useMagic(p, "reraiseException") useMagic(p, "reraiseException")
line(p, "else {" & tnl) line(p, "else {" & tnl)
line(p, indent & "reraiseException();" & tnl) line(p, "\treraiseException();" & tnl)
line(p, "}" & tnl) line(p, "}" & tnl)
addf(p.body, "$1lastJSError = $1prevJSError;$n", [dollar]) addf(p.body, "$1lastJSError = $1prevJSError;$n", [dollar])
line(p, "} finally {" & tnl) line(p, "} finally {" & tnl)
@ -701,7 +702,7 @@ proc genCaseJS(p: PProc, n: PNode, r: var TCompRes) =
case e.kind case e.kind
of nkStrLit..nkTripleStrLit: lineF(p, "case $1:$n", of nkStrLit..nkTripleStrLit: lineF(p, "case $1:$n",
[makeJSString(e.strVal, false)]) [makeJSString(e.strVal, false)])
else: internalError(e.info, "jsgen.genCaseStmt: 2") else: internalError(p.config, e.info, "jsgen.genCaseStmt: 2")
else: else:
gen(p, e, cond) gen(p, e, cond)
lineF(p, "case $1:$n", [cond.rdLoc]) lineF(p, "case $1:$n", [cond.rdLoc])
@ -715,7 +716,7 @@ proc genCaseJS(p: PProc, n: PNode, r: var TCompRes) =
gen(p, it.sons[0], stmt) gen(p, it.sons[0], stmt)
moveInto(p, stmt, r) moveInto(p, stmt, r)
lineF(p, "break;$n", []) lineF(p, "break;$n", [])
else: internalError(it.info, "jsgen.genCaseStmt") else: internalError(p.config, it.info, "jsgen.genCaseStmt")
lineF(p, "}$n", []) lineF(p, "}$n", [])
proc genBlock(p: PProc, n: PNode, r: var TCompRes) = proc genBlock(p: PProc, n: PNode, r: var TCompRes) =
@ -723,7 +724,7 @@ proc genBlock(p: PProc, n: PNode, r: var TCompRes) =
let idx = len(p.blocks) let idx = len(p.blocks)
if n.sons[0].kind != nkEmpty: if n.sons[0].kind != nkEmpty:
# named block? # named block?
if (n.sons[0].kind != nkSym): internalError(n.info, "genBlock") if (n.sons[0].kind != nkSym): internalError(p.config, n.info, "genBlock")
var sym = n.sons[0].sym var sym = n.sons[0].sym
sym.loc.k = locOther sym.loc.k = locOther
sym.position = idx+1 sym.position = idx+1
@ -749,7 +750,7 @@ proc genBreakStmt(p: PProc, n: PNode) =
idx = len(p.blocks) - 1 idx = len(p.blocks) - 1
while idx >= 0 and not p.blocks[idx].isLoop: dec idx while idx >= 0 and not p.blocks[idx].isLoop: dec idx
if idx < 0 or not p.blocks[idx].isLoop: if idx < 0 or not p.blocks[idx].isLoop:
internalError(n.info, "no loop to break") internalError(p.config, n.info, "no loop to break")
p.blocks[idx].id = abs(p.blocks[idx].id) # label is used p.blocks[idx].id = abs(p.blocks[idx].id) # label is used
lineF(p, "break L$1;$n", [rope(p.blocks[idx].id)]) lineF(p, "break L$1;$n", [rope(p.blocks[idx].id)])
@ -874,7 +875,7 @@ proc genAsgnAux(p: PProc, x, y: PNode, noCopyNeeded: bool) =
elif b.typ == etyBaseIndex: elif b.typ == etyBaseIndex:
lineF(p, "$# = $#;$n", [a.res, b.rdLoc]) lineF(p, "$# = $#;$n", [a.res, b.rdLoc])
else: else:
internalError(x.info, "genAsgn") internalError(p.config, x.info, "genAsgn")
else: else:
lineF(p, "$1 = $2; $3 = $4;$n", [a.address, b.address, a.res, b.res]) lineF(p, "$1 = $2; $3 = $4;$n", [a.address, b.address, a.res, b.res])
else: else:
@ -904,18 +905,18 @@ proc genSwap(p: PProc, n: PNode) =
if mapType(p, skipTypes(n.sons[1].typ, abstractVar)) == etyBaseIndex: if mapType(p, skipTypes(n.sons[1].typ, abstractVar)) == etyBaseIndex:
let tmp2 = p.getTemp(false) let tmp2 = p.getTemp(false)
if a.typ != etyBaseIndex or b.typ != etyBaseIndex: if a.typ != etyBaseIndex or b.typ != etyBaseIndex:
internalError(n.info, "genSwap") internalError(p.config, n.info, "genSwap")
lineF(p, "var $1 = $2; $2 = $3; $3 = $1;$n", lineF(p, "var $1 = $2; $2 = $3; $3 = $1;$n",
[tmp, a.address, b.address]) [tmp, a.address, b.address])
tmp = tmp2 tmp = tmp2
lineF(p, "var $1 = $2; $2 = $3; $3 = $1;", lineF(p, "var $1 = $2; $2 = $3; $3 = $1;",
[tmp, a.res, b.res]) [tmp, a.res, b.res])
proc getFieldPosition(f: PNode): int = proc getFieldPosition(p: PProc; f: PNode): int =
case f.kind case f.kind
of nkIntLit..nkUInt64Lit: result = int(f.intVal) of nkIntLit..nkUInt64Lit: result = int(f.intVal)
of nkSym: result = f.sym.position of nkSym: result = f.sym.position
else: internalError(f.info, "genFieldPosition") else: internalError(p.config, f.info, "genFieldPosition")
proc genFieldAddr(p: PProc, n: PNode, r: var TCompRes) = proc genFieldAddr(p: PProc, n: PNode, r: var TCompRes) =
var a: TCompRes var a: TCompRes
@ -923,13 +924,13 @@ proc genFieldAddr(p: PProc, n: PNode, r: var TCompRes) =
let b = if n.kind == nkHiddenAddr: n.sons[0] else: n let b = if n.kind == nkHiddenAddr: n.sons[0] else: n
gen(p, b.sons[0], a) gen(p, b.sons[0], a)
if skipTypes(b.sons[0].typ, abstractVarRange).kind == tyTuple: if skipTypes(b.sons[0].typ, abstractVarRange).kind == tyTuple:
r.res = makeJSString("Field" & $getFieldPosition(b.sons[1])) r.res = makeJSString("Field" & $getFieldPosition(p, b.sons[1]))
else: else:
if b.sons[1].kind != nkSym: internalError(b.sons[1].info, "genFieldAddr") if b.sons[1].kind != nkSym: internalError(p.config, b.sons[1].info, "genFieldAddr")
var f = b.sons[1].sym var f = b.sons[1].sym
if f.loc.r == nil: f.loc.r = mangleName(p.module, f) if f.loc.r == nil: f.loc.r = mangleName(p.module, f)
r.res = makeJSString($f.loc.r) r.res = makeJSString($f.loc.r)
internalAssert a.typ != etyBaseIndex internalAssert p.config, a.typ != etyBaseIndex
r.address = a.res r.address = a.res
r.kind = resExpr r.kind = resExpr
@ -939,9 +940,9 @@ proc genFieldAccess(p: PProc, n: PNode, r: var TCompRes) =
let otyp = skipTypes(n.sons[0].typ, abstractVarRange) let otyp = skipTypes(n.sons[0].typ, abstractVarRange)
if otyp.kind == tyTuple: if otyp.kind == tyTuple:
r.res = ("$1.Field$2") % r.res = ("$1.Field$2") %
[r.res, getFieldPosition(n.sons[1]).rope] [r.res, getFieldPosition(p, n.sons[1]).rope]
else: else:
if n.sons[1].kind != nkSym: internalError(n.sons[1].info, "genFieldAccess") if n.sons[1].kind != nkSym: internalError(p.config, n.sons[1].info, "genFieldAccess")
var f = n.sons[1].sym var f = n.sons[1].sym
if f.loc.r == nil: f.loc.r = mangleName(p.module, f) if f.loc.r == nil: f.loc.r = mangleName(p.module, f)
r.res = "$1.$2" % [r.res, f.loc.r] r.res = "$1.$2" % [r.res, f.loc.r]
@ -951,7 +952,7 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes)
proc genCheckedFieldAddr(p: PProc, n: PNode, r: var TCompRes) = proc genCheckedFieldAddr(p: PProc, n: PNode, r: var TCompRes) =
let m = if n.kind == nkHiddenAddr: n.sons[0] else: n let m = if n.kind == nkHiddenAddr: n.sons[0] else: n
internalAssert m.kind == nkCheckedFieldExpr internalAssert p.config, m.kind == nkCheckedFieldExpr
genAddr(p, m, r) # XXX genAddr(p, m, r) # XXX
proc genCheckedFieldAccess(p: PProc, n: PNode, r: var TCompRes) = proc genCheckedFieldAccess(p: PProc, n: PNode, r: var TCompRes) =
@ -965,7 +966,7 @@ proc genArrayAddr(p: PProc, n: PNode, r: var TCompRes) =
let m = if n.kind == nkHiddenAddr: n.sons[0] else: n let m = if n.kind == nkHiddenAddr: n.sons[0] else: n
gen(p, m.sons[0], a) gen(p, m.sons[0], a)
gen(p, m.sons[1], b) gen(p, m.sons[1], b)
internalAssert a.typ != etyBaseIndex and b.typ != etyBaseIndex internalAssert p.config, a.typ != etyBaseIndex and b.typ != etyBaseIndex
r.address = a.res r.address = a.res
var typ = skipTypes(m.sons[0].typ, abstractPtrs) var typ = skipTypes(m.sons[0].typ, abstractPtrs)
if typ.kind == tyArray: first = firstOrd(typ.sons[0]) if typ.kind == tyArray: first = firstOrd(typ.sons[0])
@ -987,9 +988,9 @@ proc genArrayAccess(p: PProc, n: PNode, r: var TCompRes) =
genArrayAddr(p, n, r) genArrayAddr(p, n, r)
of tyTuple: of tyTuple:
genFieldAddr(p, n, r) genFieldAddr(p, n, r)
else: internalError(n.info, "expr(nkBracketExpr, " & $ty.kind & ')') else: internalError(p.config, n.info, "expr(nkBracketExpr, " & $ty.kind & ')')
r.typ = etyNone r.typ = etyNone
if r.res == nil: internalError(n.info, "genArrayAccess") if r.res == nil: internalError(p.config, n.info, "genArrayAccess")
if ty.kind == tyCString: if ty.kind == tyCString:
r.res = "$1.charCodeAt($2)" % [r.address, r.res] r.res = "$1.charCodeAt($2)" % [r.address, r.res]
else: else:
@ -1009,7 +1010,7 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
case n.sons[0].kind case n.sons[0].kind
of nkSym: of nkSym:
let s = n.sons[0].sym let s = n.sons[0].sym
if s.loc.r == nil: internalError(n.info, "genAddr: 3") if s.loc.r == nil: internalError(p.config, n.info, "genAddr: 3")
case s.kind case s.kind
of skVar, skLet, skResult: of skVar, skLet, skResult:
r.kind = resExpr r.kind = resExpr
@ -1031,8 +1032,8 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
else: else:
# 'var openArray' for instance produces an 'addr' but this is harmless: # 'var openArray' for instance produces an 'addr' but this is harmless:
gen(p, n.sons[0], r) gen(p, n.sons[0], r)
#internalError(n.info, "genAddr: 4 " & renderTree(n)) #internalError(p.config, n.info, "genAddr: 4 " & renderTree(n))
else: internalError(n.info, "genAddr: 2") else: internalError(p.config, n.info, "genAddr: 2")
of nkCheckedFieldExpr: of nkCheckedFieldExpr:
genCheckedFieldAddr(p, n, r) genCheckedFieldAddr(p, n, r)
of nkDotExpr: of nkDotExpr:
@ -1051,12 +1052,12 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
genArrayAddr(p, n.sons[0], r) genArrayAddr(p, n.sons[0], r)
of tyTuple: of tyTuple:
genFieldAddr(p, n.sons[0], r) genFieldAddr(p, n.sons[0], r)
else: internalError(n.sons[0].info, "expr(nkBracketExpr, " & $kindOfIndexedExpr & ')') else: internalError(p.config, n.sons[0].info, "expr(nkBracketExpr, " & $kindOfIndexedExpr & ')')
of nkObjDownConv: of nkObjDownConv:
gen(p, n.sons[0], r) gen(p, n.sons[0], r)
of nkHiddenDeref: of nkHiddenDeref:
gen(p, n.sons[0].sons[0], r) gen(p, n.sons[0].sons[0], r)
else: internalError(n.sons[0].info, "genAddr: " & $n.sons[0].kind) else: internalError(p.config, n.sons[0].info, "genAddr: " & $n.sons[0].kind)
proc thisParam(p: PProc; typ: PType): PType = proc thisParam(p: PProc; typ: PType): PType =
discard discard
@ -1090,7 +1091,7 @@ proc genSym(p: PProc, n: PNode, r: var TCompRes) =
case s.kind case s.kind
of skVar, skLet, skParam, skTemp, skResult, skForVar: of skVar, skLet, skParam, skTemp, skResult, skForVar:
if s.loc.r == nil: if s.loc.r == nil:
internalError(n.info, "symbol has no generated name: " & s.name.s) internalError(p.config, n.info, "symbol has no generated name: " & s.name.s)
let k = mapType(p, s.typ) let k = mapType(p, s.typ)
if k == etyBaseIndex: if k == etyBaseIndex:
r.typ = etyBaseIndex r.typ = etyBaseIndex
@ -1107,7 +1108,7 @@ proc genSym(p: PProc, n: PNode, r: var TCompRes) =
of skConst: of skConst:
genConstant(p, s) genConstant(p, s)
if s.loc.r == nil: if s.loc.r == nil:
internalError(n.info, "symbol has no generated name: " & s.name.s) internalError(p.config, n.info, "symbol has no generated name: " & s.name.s)
r.res = s.loc.r r.res = s.loc.r
of skProc, skFunc, skConverter, skMethod: of skProc, skFunc, skConverter, skMethod:
discard mangleName(p.module, s) discard mangleName(p.module, s)
@ -1124,7 +1125,7 @@ proc genSym(p: PProc, n: PNode, r: var TCompRes) =
genProcForSymIfNeeded(p, s) genProcForSymIfNeeded(p, s)
else: else:
if s.loc.r == nil: if s.loc.r == nil:
internalError(n.info, "symbol has no generated name: " & s.name.s) internalError(p.config, n.info, "symbol has no generated name: " & s.name.s)
r.res = s.loc.r r.res = s.loc.r
r.kind = resVal r.kind = resVal
@ -1145,7 +1146,7 @@ proc genDeref(p: PProc, n: PNode, r: var TCompRes) =
elif t == etyBaseIndex: elif t == etyBaseIndex:
r.res = "$1[0]" % [a.res] r.res = "$1[0]" % [a.res]
else: else:
internalError(n.info, "genDeref") internalError(p.config, n.info, "genDeref")
proc genArgNoParam(p: PProc, n: PNode, r: var TCompRes) = proc genArgNoParam(p: PProc, n: PNode, r: var TCompRes) =
var a: TCompRes var a: TCompRes
@ -1205,14 +1206,14 @@ proc genArgs(p: PProc, n: PNode, r: var TCompRes; start=1) =
# XXX look into this: # XXX look into this:
let jsp = countJsParams(typ) let jsp = countJsParams(typ)
if emitted != jsp and tfVarargs notin typ.flags: if emitted != jsp and tfVarargs notin typ.flags:
localError(n.info, "wrong number of parameters emitted; expected: " & $jsp & localError(p.config, n.info, "wrong number of parameters emitted; expected: " & $jsp &
" but got: " & $emitted) " but got: " & $emitted)
r.kind = resExpr r.kind = resExpr
proc genOtherArg(p: PProc; n: PNode; i: int; typ: PType; proc genOtherArg(p: PProc; n: PNode; i: int; typ: PType;
generated: var int; r: var TCompRes) = generated: var int; r: var TCompRes) =
if i >= n.len: if i >= n.len:
globalError(n.info, "wrong importcpp pattern; expected parameter at position " & $i & globalError(p.config, n.info, "wrong importcpp pattern; expected parameter at position " & $i &
" but got only: " & $(n.len-1)) " but got only: " & $(n.len-1))
let it = n[i] let it = n[i]
var paramType: PNode = nil var paramType: PNode = nil
@ -1266,7 +1267,7 @@ proc genInfixCall(p: PProc, n: PNode, r: var TCompRes) =
if f.loc.r == nil: f.loc.r = mangleName(p.module, f) if f.loc.r == nil: f.loc.r = mangleName(p.module, f)
if sfInfixCall in f.flags: if sfInfixCall in f.flags:
let pat = n.sons[0].sym.loc.r.data let pat = n.sons[0].sym.loc.r.data
internalAssert pat != nil internalAssert p.config, pat != nil
if pat.contains({'#', '(', '@'}): if pat.contains({'#', '(', '@'}):
var typ = skipTypes(n.sons[0].typ, abstractInst) var typ = skipTypes(n.sons[0].typ, abstractInst)
assert(typ.kind == tyProc) assert(typ.kind == tyProc)
@ -1276,7 +1277,7 @@ proc genInfixCall(p: PProc, n: PNode, r: var TCompRes) =
gen(p, n.sons[1], r) gen(p, n.sons[1], r)
if r.typ == etyBaseIndex: if r.typ == etyBaseIndex:
if r.address == nil: if r.address == nil:
globalError(n.info, "cannot invoke with infix syntax") globalError(p.config, n.info, "cannot invoke with infix syntax")
r.res = "$1[$2]" % [r.address, r.res] r.res = "$1[$2]" % [r.address, r.res]
r.address = nil r.address = nil
r.typ = etyNone r.typ = etyNone
@ -1295,7 +1296,7 @@ proc genCall(p: PProc, n: PNode, r: var TCompRes) =
proc genEcho(p: PProc, n: PNode, r: var TCompRes) = proc genEcho(p: PProc, n: PNode, r: var TCompRes) =
let n = n[1].skipConv let n = n[1].skipConv
internalAssert n.kind == nkBracket internalAssert p.config, n.kind == nkBracket
useMagic(p, "toJSStr") # Used in rawEcho useMagic(p, "toJSStr") # Used in rawEcho
useMagic(p, "rawEcho") useMagic(p, "rawEcho")
add(r.res, "rawEcho(") add(r.res, "rawEcho(")
@ -1326,7 +1327,7 @@ proc createRecordVarAux(p: PProc, rec: PNode, excludedFieldIDs: IntSet, output:
if output.len > 0: output.add(", ") if output.len > 0: output.add(", ")
output.addf("$#: ", [mangleName(p.module, rec.sym)]) output.addf("$#: ", [mangleName(p.module, rec.sym)])
output.add(createVar(p, rec.sym.typ, false)) output.add(createVar(p, rec.sym.typ, false))
else: internalError(rec.info, "createRecordVarAux") else: internalError(p.config, rec.info, "createRecordVarAux")
proc createObjInitList(p: PProc, typ: PType, excludedFieldIDs: IntSet, output: var Rope) = proc createObjInitList(p: PProc, typ: PType, excludedFieldIDs: IntSet, output: var Rope) =
var t = typ var t = typ
@ -1409,10 +1410,10 @@ proc createVar(p: PProc, typ: PType, indirect: bool): Rope =
if t.n != nil: if t.n != nil:
result = createVar(p, lastSon t, indirect) result = createVar(p, lastSon t, indirect)
else: else:
internalError("createVar: " & $t.kind) internalError(p.config, "createVar: " & $t.kind)
result = nil result = nil
else: else:
internalError("createVar: " & $t.kind) internalError(p.config, "createVar: " & $t.kind)
result = nil result = nil
template returnType: untyped = template returnType: untyped =
@ -1482,7 +1483,7 @@ proc genVarStmt(p: PProc, n: PNode) =
var a = n.sons[i] var a = n.sons[i]
if a.kind != nkCommentStmt: if a.kind != nkCommentStmt:
if a.kind == nkVarTuple: if a.kind == nkVarTuple:
let unpacked = lowerTupleUnpacking(a, p.prc) let unpacked = lowerTupleUnpacking(p.module.graph, a, p.prc)
genStmt(p, unpacked) genStmt(p, unpacked)
else: else:
assert(a.kind == nkIdentDefs) assert(a.kind == nkIdentDefs)
@ -1519,7 +1520,7 @@ proc genOrd(p: PProc, n: PNode, r: var TCompRes) =
case skipTypes(n.sons[1].typ, abstractVar).kind case skipTypes(n.sons[1].typ, abstractVar).kind
of tyEnum, tyInt..tyUInt64, tyChar: gen(p, n.sons[1], r) of tyEnum, tyInt..tyUInt64, tyChar: gen(p, n.sons[1], r)
of tyBool: unaryExpr(p, n, r, "", "($1 ? 1:0)") of tyBool: unaryExpr(p, n, r, "", "($1 ? 1:0)")
else: internalError(n.info, "genOrd") else: internalError(p.config, n.info, "genOrd")
proc genConStrStr(p: PProc, n: PNode, r: var TCompRes) = proc genConStrStr(p: PProc, n: PNode, r: var TCompRes) =
var a: TCompRes var a: TCompRes
@ -1559,9 +1560,9 @@ proc genToArray(p: PProc; n: PNode; r: var TCompRes) =
gen(p, it[1], b) gen(p, it[1], b)
r.res.add("$# => $#" % [a.rdLoc, b.rdLoc]) r.res.add("$# => $#" % [a.rdLoc, b.rdLoc])
else: else:
localError(it.info, "'toArray' needs tuple constructors") localError(p.config, it.info, "'toArray' needs tuple constructors")
else: else:
localError(x.info, "'toArray' needs an array literal") localError(p.config, x.info, "'toArray' needs an array literal")
r.res.add(")") r.res.add(")")
proc genReprAux(p: PProc, n: PNode, r: var TCompRes, magic: string, typ: Rope = nil) = proc genReprAux(p: PProc, n: PNode, r: var TCompRes, magic: string, typ: Rope = nil) =
@ -1604,7 +1605,7 @@ proc genRepr(p: PProc, n: PNode, r: var TCompRes) =
of tySet: of tySet:
genReprAux(p, n, r, "reprSet", genTypeInfo(p, t)) genReprAux(p, n, r, "reprSet", genTypeInfo(p, t))
of tyEmpty, tyVoid: of tyEmpty, tyVoid:
localError(n.info, "'repr' doesn't support 'void' type") localError(p.config, n.info, "'repr' doesn't support 'void' type")
of tyPointer: of tyPointer:
genReprAux(p, n, r, "reprPointer") genReprAux(p, n, r, "reprPointer")
of tyOpenArray, tyVarargs: of tyOpenArray, tyVarargs:
@ -1736,7 +1737,7 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
of mReset: genReset(p, n) of mReset: genReset(p, n)
of mEcho: genEcho(p, n, r) of mEcho: genEcho(p, n, r)
of mNLen..mNError, mSlurp, mStaticExec: of mNLen..mNError, mSlurp, mStaticExec:
localError(n.info, errXMustBeCompileTime, n.sons[0].sym.name.s) localError(p.config, n.info, errXMustBeCompileTime % n.sons[0].sym.name.s)
of mCopyStr: of mCopyStr:
binaryExpr(p, n, r, "", "($1.slice($2))") binaryExpr(p, n, r, "", "($1.slice($2))")
of mCopyStrLast: of mCopyStrLast:
@ -1754,7 +1755,7 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
genCall(p, n, r) genCall(p, n, r)
else: else:
genCall(p, n, r) genCall(p, n, r)
#else internalError(e.info, 'genMagic: ' + magicToStr[op]); #else internalError(p.config, e.info, 'genMagic: ' + magicToStr[op]);
proc genSetConstr(p: PProc, n: PNode, r: var TCompRes) = proc genSetConstr(p: PProc, n: PNode, r: var TCompRes) =
var var
@ -1810,7 +1811,7 @@ proc genObjConstr(p: PProc, n: PNode, r: var TCompRes) =
for i in countup(1, sonsLen(n) - 1): for i in countup(1, sonsLen(n) - 1):
if i > 1: add(initList, ", ") if i > 1: add(initList, ", ")
var it = n.sons[i] var it = n.sons[i]
internalAssert it.kind == nkExprColonExpr internalAssert p.config, it.kind == nkExprColonExpr
let val = it.sons[1] let val = it.sons[1]
gen(p, val, a) gen(p, val, a)
var f = it.sons[0].sym var f = it.sons[0].sym
@ -1866,7 +1867,7 @@ proc convStrToCStr(p: PProc, n: PNode, r: var TCompRes) =
gen(p, n.sons[0].sons[0], r) gen(p, n.sons[0].sons[0], r)
else: else:
gen(p, n.sons[0], r) gen(p, n.sons[0], r)
if r.res == nil: internalError(n.info, "convStrToCStr") if r.res == nil: internalError(p.config, n.info, "convStrToCStr")
useMagic(p, "toJSStr") useMagic(p, "toJSStr")
r.res = "toJSStr($1)" % [r.res] r.res = "toJSStr($1)" % [r.res]
r.kind = resExpr r.kind = resExpr
@ -1878,13 +1879,13 @@ proc convCStrToStr(p: PProc, n: PNode, r: var TCompRes) =
gen(p, n.sons[0].sons[0], r) gen(p, n.sons[0].sons[0], r)
else: else:
gen(p, n.sons[0], r) gen(p, n.sons[0], r)
if r.res == nil: internalError(n.info, "convCStrToStr") if r.res == nil: internalError(p.config, n.info, "convCStrToStr")
useMagic(p, "cstrToNimstr") useMagic(p, "cstrToNimstr")
r.res = "cstrToNimstr($1)" % [r.res] r.res = "cstrToNimstr($1)" % [r.res]
r.kind = resExpr r.kind = resExpr
proc genReturnStmt(p: PProc, n: PNode) = proc genReturnStmt(p: PProc, n: PNode) =
if p.procDef == nil: internalError(n.info, "genReturnStmt") if p.procDef == nil: internalError(p.config, n.info, "genReturnStmt")
p.beforeRetNeeded = true p.beforeRetNeeded = true
if n.sons[0].kind != nkEmpty: if n.sons[0].kind != nkEmpty:
genStmt(p, n.sons[0]) genStmt(p, n.sons[0])
@ -2140,7 +2141,7 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) =
of nkVarSection, nkLetSection: genVarStmt(p, n) of nkVarSection, nkLetSection: genVarStmt(p, n)
of nkConstSection: discard of nkConstSection: discard
of nkForStmt, nkParForStmt: of nkForStmt, nkParForStmt:
internalError(n.info, "for statement not eliminated") internalError(p.config, n.info, "for statement not eliminated")
of nkCaseStmt: genCaseJS(p, n, r) of nkCaseStmt: genCaseJS(p, n, r)
of nkReturnStmt: genReturnStmt(p, n) of nkReturnStmt: genReturnStmt(p, n)
of nkBreakStmt: genBreakStmt(p, n) of nkBreakStmt: genBreakStmt(p, n)
@ -2163,13 +2164,13 @@ proc gen(p: PProc, n: PNode, r: var TCompRes) =
genSym(p, n.sons[namePos], r) genSym(p, n.sons[namePos], r)
r.res = nil r.res = nil
of nkGotoState, nkState: of nkGotoState, nkState:
internalError(n.info, "first class iterators not implemented") internalError(p.config, n.info, "first class iterators not implemented")
of nkPragmaBlock: gen(p, n.lastSon, r) of nkPragmaBlock: gen(p, n.lastSon, r)
of nkComesFrom: of nkComesFrom:
discard "XXX to implement for better stack traces" discard "XXX to implement for better stack traces"
else: internalError(n.info, "gen: unknown node type: " & $n.kind) else: internalError(p.config, n.info, "gen: unknown node type: " & $n.kind)
var globals: PGlobals var globals: PGlobals # XXX global variable here
proc newModule(module: PSym): BModule = proc newModule(module: PSym): BModule =
new(result) new(result)
@ -2205,10 +2206,10 @@ proc genModule(p: PProc, n: PNode) =
add(p.body, frameDestroy(p)) add(p.body, frameDestroy(p))
proc myProcess(b: PPassContext, n: PNode): PNode = proc myProcess(b: PPassContext, n: PNode): PNode =
if passes.skipCodegen(n): return n
result = n result = n
var m = BModule(b) let m = BModule(b)
if m.module == nil: internalError(n.info, "myProcess") if passes.skipCodegen(m.config, n): return n
if m.module == nil: internalError(m.config, n.info, "myProcess")
var p = newProc(globals, m, nil, m.module.options) var p = newProc(globals, m, nil, m.module.options)
p.unique = globals.unique p.unique = globals.unique
genModule(p, n) genModule(p, n)
@ -2235,11 +2236,11 @@ proc getClassName(t: PType): Rope =
if s.isNil or sfAnon in s.flags: if s.isNil or sfAnon in s.flags:
s = skipTypes(t, abstractPtrs).sym s = skipTypes(t, abstractPtrs).sym
if s.isNil or sfAnon in s.flags: if s.isNil or sfAnon in s.flags:
internalError("cannot retrieve class name") doAssert(false, "cannot retrieve class name")
if s.loc.r != nil: result = s.loc.r if s.loc.r != nil: result = s.loc.r
else: result = rope(s.name.s) else: result = rope(s.name.s)
proc genClass(obj: PType; content: Rope; ext: string) = proc genClass(conf: ConfigRef; obj: PType; content: Rope; ext: string) =
let cls = getClassName(obj) let cls = getClassName(obj)
let t = skipTypes(obj, abstractPtrs) let t = skipTypes(obj, abstractPtrs)
let extends = if t.kind == tyObject and t.sons[0] != nil: let extends = if t.kind == tyObject and t.sons[0] != nil:
@ -2252,34 +2253,36 @@ proc genClass(obj: PType; content: Rope; ext: string) =
"class $#$# {$n$#$n}$n") % "class $#$# {$n$#$n}$n") %
[rope(VersionAsString), cls, extends, content] [rope(VersionAsString), cls, extends, content]
let outfile = changeFileExt(completeCFilePath($cls), ext) let outfile = changeFileExt(completeCFilePath(conf, $cls), ext)
discard writeRopeIfNotEqual(result, outfile) discard writeRopeIfNotEqual(result, outfile)
proc myClose(graph: ModuleGraph; b: PPassContext, n: PNode): PNode = proc myClose(graph: ModuleGraph; b: PPassContext, n: PNode): PNode =
if passes.skipCodegen(n): return n
result = myProcess(b, n) result = myProcess(b, n)
var m = BModule(b) var m = BModule(b)
if passes.skipCodegen(m.config, n): return n
if sfMainModule in m.module.flags: if sfMainModule in m.module.flags:
let ext = "js" let ext = "js"
let f = if globals.classes.len == 0: toFilename(FileIndex m.module.position) let f = if globals.classes.len == 0: toFilename(FileIndex m.module.position)
else: "nimsystem" else: "nimsystem"
let code = wholeCode(graph, m) let code = wholeCode(graph, m)
let outfile = let outfile =
if options.outFile.len > 0: if m.config.outFile.len > 0:
if options.outFile.isAbsolute: options.outFile if m.config.outFile.isAbsolute: m.config.outFile
else: getCurrentDir() / options.outFile else: getCurrentDir() / m.config.outFile
else: else:
changeFileExt(completeCFilePath(f), ext) changeFileExt(completeCFilePath(m.config, f), ext)
discard writeRopeIfNotEqual(genHeader() & code, outfile) discard writeRopeIfNotEqual(genHeader() & code, outfile)
for obj, content in items(globals.classes): for obj, content in items(globals.classes):
genClass(obj, content, ext) genClass(m.config, obj, content, ext)
proc myOpenCached(graph: ModuleGraph; s: PSym, rd: PRodReader): PPassContext = proc myOpenCached(graph: ModuleGraph; s: PSym, rd: PRodReader): PPassContext =
internalError("symbol files are not possible with the JS code generator") internalError(graph.config, "symbol files are not possible with the JS code generator")
result = nil result = nil
proc myOpen(graph: ModuleGraph; s: PSym; cache: IdentCache): PPassContext = proc myOpen(graph: ModuleGraph; s: PSym; cache: IdentCache): PPassContext =
var r = newModule(s) var r = newModule(s)
r.graph = graph
r.config = graph.config
result = r result = r
const JSgenPass* = makePass(myOpen, myOpenCached, myProcess, myClose) const JSgenPass* = makePass(myOpen, myOpenCached, myProcess, myClose)

View file

@ -38,7 +38,7 @@ proc genObjectFields(p: PProc, typ: PType, n: PNode): Rope =
makeJSString(field.name.s)] makeJSString(field.name.s)]
of nkRecCase: of nkRecCase:
length = sonsLen(n) length = sonsLen(n)
if (n.sons[0].kind != nkSym): internalError(n.info, "genObjectFields") if (n.sons[0].kind != nkSym): internalError(p.config, n.info, "genObjectFields")
field = n.sons[0].sym field = n.sons[0].sym
s = genTypeInfo(p, field.typ) s = genTypeInfo(p, field.typ)
for i in countup(1, length - 1): for i in countup(1, length - 1):
@ -47,7 +47,7 @@ proc genObjectFields(p: PProc, typ: PType, n: PNode): Rope =
case b.kind case b.kind
of nkOfBranch: of nkOfBranch:
if sonsLen(b) < 2: if sonsLen(b) < 2:
internalError(b.info, "genObjectFields; nkOfBranch broken") internalError(p.config, b.info, "genObjectFields; nkOfBranch broken")
for j in countup(0, sonsLen(b) - 2): for j in countup(0, sonsLen(b) - 2):
if u != nil: add(u, ", ") if u != nil: add(u, ", ")
if b.sons[j].kind == nkRange: if b.sons[j].kind == nkRange:
@ -57,7 +57,7 @@ proc genObjectFields(p: PProc, typ: PType, n: PNode): Rope =
add(u, rope(getOrdValue(b.sons[j]))) add(u, rope(getOrdValue(b.sons[j])))
of nkElse: of nkElse:
u = rope(lengthOrd(field.typ)) u = rope(lengthOrd(field.typ))
else: internalError(n.info, "genObjectFields(nkRecCase)") else: internalError(p.config, n.info, "genObjectFields(nkRecCase)")
if result != nil: add(result, ", " & tnl) if result != nil: add(result, ", " & tnl)
addf(result, "[setConstr($1), $2]", addf(result, "[setConstr($1), $2]",
[u, genObjectFields(p, typ, lastSon(b))]) [u, genObjectFields(p, typ, lastSon(b))])
@ -65,7 +65,7 @@ proc genObjectFields(p: PProc, typ: PType, n: PNode): Rope =
"typ: $2, name: $4, sons: [$5]}") % [ "typ: $2, name: $4, sons: [$5]}") % [
mangleName(p.module, field), s, mangleName(p.module, field), s,
rope(lengthOrd(field.typ)), makeJSString(field.name.s), result] rope(lengthOrd(field.typ)), makeJSString(field.name.s), result]
else: internalError(n.info, "genObjectFields") else: internalError(p.config, n.info, "genObjectFields")
proc objHasTypeField(t: PType): bool {.inline.} = proc objHasTypeField(t: PType): bool {.inline.} =
tfInheritable in t.flags or t.sons[0] != nil tfInheritable in t.flags or t.sons[0] != nil
@ -104,7 +104,7 @@ proc genEnumInfo(p: PProc, typ: PType, name: Rope) =
let length = sonsLen(typ.n) let length = sonsLen(typ.n)
var s: Rope = nil var s: Rope = nil
for i in countup(0, length - 1): for i in countup(0, length - 1):
if (typ.n.sons[i].kind != nkSym): internalError(typ.n.info, "genEnumInfo") if (typ.n.sons[i].kind != nkSym): internalError(p.config, typ.n.info, "genEnumInfo")
let field = typ.n.sons[i].sym let field = typ.n.sons[i].sym
if i > 0: add(s, ", " & tnl) if i > 0: add(s, ", " & tnl)
let extName = if field.ast == nil: field.name.s else: field.ast.strVal let extName = if field.ast == nil: field.name.s else: field.ast.strVal
@ -152,5 +152,5 @@ proc genTypeInfo(p: PProc, typ: PType): Rope =
of tyTuple: genTupleInfo(p, t, result) of tyTuple: genTupleInfo(p, t, result)
of tyStatic: of tyStatic:
if t.n != nil: result = genTypeInfo(p, lastSon t) if t.n != nil: result = genTypeInfo(p, lastSon t)
else: internalError("genTypeInfo(" & $t.kind & ')') else: internalError(p.config, "genTypeInfo(" & $t.kind & ')')
else: internalError("genTypeInfo(" & $t.kind & ')') else: internalError(p.config, "genTypeInfo(" & $t.kind & ')')

View file

@ -11,10 +11,11 @@
import import
ast, astalgo, magicsys, std / sha1, rodread, msgs, cgendata, sigmatch, options, ast, astalgo, magicsys, std / sha1, rodread, msgs, cgendata, sigmatch, options,
idents, os, lexer, idgen, passes, syntaxes, llstream, modulegraphs, rod idents, os, lexer, idgen, passes, syntaxes, llstream, modulegraphs, rod,
configuration
proc resetSystemArtifacts*() = proc resetSystemArtifacts*(g: ModuleGraph) =
magicsys.resetSysTypes() magicsys.resetSysTypes(g)
proc newModule(graph: ModuleGraph; fileIdx: FileIndex): PSym = proc newModule(graph: ModuleGraph; fileIdx: FileIndex): PSym =
# We cannot call ``newSym`` here, because we have to circumvent the ID # We cannot call ``newSym`` here, because we have to circumvent the ID
@ -25,11 +26,11 @@ proc newModule(graph: ModuleGraph; fileIdx: FileIndex): PSym =
let filename = fileIdx.toFullPath let filename = fileIdx.toFullPath
result.name = getIdent(splitFile(filename).name) result.name = getIdent(splitFile(filename).name)
if not isNimIdentifier(result.name.s): if not isNimIdentifier(result.name.s):
rawMessage(errInvalidModuleName, result.name.s) rawMessage(graph.config, errGenerated, "invalid module name: " & result.name.s)
result.info = newLineInfo(fileIdx, 1, 1) result.info = newLineInfo(fileIdx, 1, 1)
let let
pck = getPackageName(filename) pck = getPackageName(graph.config, filename)
pck2 = if pck.len > 0: pck else: "unknown" pck2 = if pck.len > 0: pck else: "unknown"
pack = getIdent(pck2) pack = getIdent(pck2)
var packSym = graph.packageSyms.strTableGet(pack) var packSym = graph.packageSyms.strTableGet(pack)
@ -49,7 +50,7 @@ proc newModule(graph: ModuleGraph; fileIdx: FileIndex): PSym =
strTableAdd(result.tab, result) # a module knows itself strTableAdd(result.tab, result) # a module knows itself
let existing = strTableGet(packSym.tab, result.name) let existing = strTableGet(packSym.tab, result.name)
if existing != nil and existing.info.fileIndex != result.info.fileIndex: if existing != nil and existing.info.fileIndex != result.info.fileIndex:
localError(result.info, "module names need to be unique per Nimble package; module clashes with " & existing.info.fileIndex.toFullPath) localError(graph.config, result.info, "module names need to be unique per Nimble package; module clashes with " & existing.info.fileIndex.toFullPath)
# strTableIncl() for error corrections: # strTableIncl() for error corrections:
discard strTableIncl(packSym.tab, result) discard strTableIncl(packSym.tab, result)
@ -74,7 +75,7 @@ proc compileModule*(graph: ModuleGraph; fileIdx: FileIndex; cache: IdentCache, f
discard discard
result.id = getModuleId(fileIdx, toFullPath(fileIdx)) result.id = getModuleId(fileIdx, toFullPath(fileIdx))
discard processModule(graph, result, discard processModule(graph, result,
if sfMainModule in flags and gProjectIsStdin: stdin.llStreamOpen else: nil, if sfMainModule in flags and graph.config.projectIsStdin: stdin.llStreamOpen else: nil,
rd, cache) rd, cache)
#if optCaasEnabled in gGlobalOptions: #if optCaasEnabled in gGlobalOptions:
# gMemCacheData[fileIdx].needsRecompile = Recompiled # gMemCacheData[fileIdx].needsRecompile = Recompiled
@ -85,7 +86,7 @@ proc compileModule*(graph: ModuleGraph; fileIdx: FileIndex; cache: IdentCache, f
initStrTable(result.tab) initStrTable(result.tab)
result.ast = nil result.ast = nil
discard processModule(graph, result, discard processModule(graph, result,
if sfMainModule in flags and gProjectIsStdin: stdin.llStreamOpen else: nil, if sfMainModule in flags and graph.config.projectIsStdin: stdin.llStreamOpen else: nil,
nil, cache) nil, cache)
graph.markClientsDirty(fileIdx) graph.markClientsDirty(fileIdx)
when false: when false:
@ -102,8 +103,9 @@ proc importModule*(graph: ModuleGraph; s: PSym, fileIdx: FileIndex;
#if sfSystemModule in result.flags: #if sfSystemModule in result.flags:
# localError(result.info, errAttemptToRedefine, result.name.s) # localError(result.info, errAttemptToRedefine, result.name.s)
# restore the notes for outer module: # restore the notes for outer module:
gNotes = if s.owner.id == gMainPackageId: gMainPackageNotes graph.config.notes =
else: ForeignPackageNotes if s.owner.id == gMainPackageId: graph.config.mainPackageNotes
else: graph.config.foreignPackageNotes
proc includeModule*(graph: ModuleGraph; s: PSym, fileIdx: FileIndex; proc includeModule*(graph: ModuleGraph; s: PSym, fileIdx: FileIndex;
cache: IdentCache): PNode {.procvar.} = cache: IdentCache): PNode {.procvar.} =
@ -112,22 +114,22 @@ proc includeModule*(graph: ModuleGraph; s: PSym, fileIdx: FileIndex;
graph.addIncludeDep(s.position.FileIndex, fileIdx) graph.addIncludeDep(s.position.FileIndex, fileIdx)
proc compileSystemModule*(graph: ModuleGraph; cache: IdentCache) = proc compileSystemModule*(graph: ModuleGraph; cache: IdentCache) =
if magicsys.systemModule == nil: if graph.systemModule == nil:
systemFileIdx = fileInfoIdx(options.libpath/"system.nim") systemFileIdx = fileInfoIdx(graph.config.libpath / "system.nim")
discard graph.compileModule(systemFileIdx, cache, {sfSystemModule}) discard graph.compileModule(systemFileIdx, cache, {sfSystemModule})
proc wantMainModule* = proc wantMainModule*(conf: ConfigRef) =
if gProjectFull.len == 0: if conf.projectFull.len == 0:
fatal(gCmdLineInfo, errCommandExpectsFilename) fatal(gCmdLineInfo, errCommandExpectsFilename)
gProjectMainIdx = int32 addFileExt(gProjectFull, NimExt).fileInfoIdx conf.projectMainIdx = int32 addFileExt(conf.projectFull, NimExt).fileInfoIdx
passes.gIncludeFile = includeModule passes.gIncludeFile = includeModule
passes.gImportModule = importModule passes.gImportModule = importModule
proc compileProject*(graph: ModuleGraph; cache: IdentCache; proc compileProject*(graph: ModuleGraph; cache: IdentCache;
projectFileIdx = InvalidFileIDX) = projectFileIdx = InvalidFileIDX) =
wantMainModule() wantMainModule(graph.config)
let systemFileIdx = fileInfoIdx(options.libpath / "system.nim") let systemFileIdx = fileInfoIdx(graph.config.libpath / "system.nim")
let projectFile = if projectFileIdx == InvalidFileIDX: FileIndex(gProjectMainIdx) else: projectFileIdx let projectFile = if projectFileIdx == InvalidFileIDX: FileIndex(gProjectMainIdx) else: projectFileIdx
graph.importStack.add projectFile graph.importStack.add projectFile
if projectFile == systemFileIdx: if projectFile == systemFileIdx:

View file

@ -10,22 +10,26 @@
## implements some little helper passes ## implements some little helper passes
import import
strutils, ast, astalgo, passes, idents, msgs, options, idgen strutils, ast, astalgo, passes, idents, msgs, options, idgen, configuration
from modulegraphs import ModuleGraph from modulegraphs import ModuleGraph
type
VerboseRef = ref object of TPassContext
config: ConfigRef
proc verboseOpen(graph: ModuleGraph; s: PSym; cache: IdentCache): PPassContext = proc verboseOpen(graph: ModuleGraph; s: PSym; cache: IdentCache): PPassContext =
#MessageOut('compiling ' + s.name.s); #MessageOut('compiling ' + s.name.s);
result = nil # we don't need a context result = VerboseRef(config: graph.config)
rawMessage(hintProcessing, s.name.s) rawMessage(graph.config, hintProcessing, s.name.s)
proc verboseProcess(context: PPassContext, n: PNode): PNode = proc verboseProcess(context: PPassContext, n: PNode): PNode =
result = n result = n
if context != nil: internalError("logpass: context is not nil") let v = VerboseRef(context)
if gVerbosity == 3: if gVerbosity == 3:
# system.nim deactivates all hints, for verbosity:3 we want the processing # system.nim deactivates all hints, for verbosity:3 we want the processing
# messages nonetheless, so we activate them again unconditionally: # messages nonetheless, so we activate them again unconditionally:
incl(msgs.gNotes, hintProcessing) incl(v.config.notes, hintProcessing)
message(n.info, hintProcessing, $idgen.gFrontendId) message(v.config, n.info, hintProcessing, $idgen.gFrontendId)
const verbosePass* = makePass(open = verboseOpen, process = verboseProcess) const verbosePass* = makePass(open = verboseOpen, process = verboseProcess)

View file

@ -11,7 +11,7 @@
import import
times, commands, options, msgs, nimconf, times, commands, options, msgs, nimconf,
extccomp, strutils, os, platform, parseopt, idents extccomp, strutils, os, platform, parseopt, idents, configuration
when useCaas: when useCaas:
import net import net
@ -42,17 +42,17 @@ proc processCmdLine*(pass: TCmdLinePass, cmd: string; config: ConfigRef) =
of cmdArgument: of cmdArgument:
if processArgument(pass, p, argsCount, config): break if processArgument(pass, p, argsCount, config): break
if pass == passCmd2: if pass == passCmd2:
if optRun notin gGlobalOptions and config.arguments.len > 0 and options.command.normalize != "run": if optRun notin gGlobalOptions and config.arguments.len > 0 and config.command.normalize != "run":
rawMessage(errArgsNeedRunOption, []) rawMessage(config, errGenerated, errArgsNeedRunOption)
proc serve*(cache: IdentCache; action: proc (cache: IdentCache){.nimcall.}; config: ConfigRef) = proc serve*(cache: IdentCache; action: proc (cache: IdentCache){.nimcall.}; config: ConfigRef) =
template execute(cmd) = template execute(cmd) =
curCaasCmd = cmd curCaasCmd = cmd
processCmdLine(passCmd2, cmd, config) processCmdLine(passCmd2, cmd, config)
action(cache) action(cache)
gErrorCounter = 0 config.errorCounter = 0
let typ = getConfigVar("server.type") let typ = getConfigVar(config, "server.type")
case typ case typ
of "stdin": of "stdin":
while true: while true:
@ -65,9 +65,9 @@ proc serve*(cache: IdentCache; action: proc (cache: IdentCache){.nimcall.}; conf
of "tcp", "": of "tcp", "":
when useCaas: when useCaas:
var server = newSocket() var server = newSocket()
let p = getConfigVar("server.port") let p = getConfigVar(config, "server.port")
let port = if p.len > 0: parseInt(p).Port else: 6000.Port let port = if p.len > 0: parseInt(p).Port else: 6000.Port
server.bindAddr(port, getConfigVar("server.address")) server.bindAddr(port, getConfigVar(config, "server.address"))
var inp = "".TaintedString var inp = "".TaintedString
server.listen() server.listen()
var stdoutSocket = newSocket() var stdoutSocket = newSocket()