Skip to content

Commit

Permalink
make case-object transitions explicit, make unknownLineInfo a const, …
Browse files Browse the repository at this point in the history
…replace a few magic numbers with consts (#13170)
  • Loading branch information
Jasper Jenkins authored and Araq committed Jan 17, 2020
1 parent 2bf337a commit 796aafe
Show file tree
Hide file tree
Showing 35 changed files with 183 additions and 204 deletions.
94 changes: 60 additions & 34 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,10 @@ const
skProcKinds* = {skProc, skFunc, skTemplate, skMacro, skIterator,
skMethod, skConverter}

defaultSize = -1
defaultAlignment = -1
defaultOffset = -1

var ggDebug* {.deprecated.}: bool ## convenience switch for trying out things
#var
# gMainPackageId*: int
Expand Down Expand Up @@ -1066,12 +1070,7 @@ when defined(useNodeIds):
var gNodeId: int

proc newNode*(kind: TNodeKind): PNode =
new(result)
result.kind = kind
#result.info = UnknownLineInfo() inlined:
result.info.fileIndex = InvalidFileIdx
result.info.col = int16(-1)
result.info.line = uint16(0)
result = PNode(kind: kind, info: unknownLineInfo)
when defined(useNodeIds):
result.id = gNodeId
if result.id == nodeIdToDebug:
Expand All @@ -1091,15 +1090,8 @@ template previouslyInferred*(t: PType): PType =
proc newSym*(symKind: TSymKind, name: PIdent, owner: PSym,
info: TLineInfo; options: TOptions = {}): PSym =
# generates a symbol and initializes the hash field too
new(result)
result.name = name
result.kind = symKind
result.flags = {}
result.info = info
result.options = options
result.owner = owner
result.offset = -1
result.id = getID()
result = PSym(name: name, kind: symKind, flags: {}, info: info, id: getID(),
options: options, owner: owner, offset: defaultOffset)
when debugIds:
registerId(result)

Expand Down Expand Up @@ -1193,9 +1185,7 @@ proc newSymNode*(sym: PSym, info: TLineInfo): PNode =
result.info = info

proc newNodeI*(kind: TNodeKind, info: TLineInfo): PNode =
new(result)
result.kind = kind
result.info = info
result = PNode(kind: kind, info: info)
when defined(useNodeIds):
result.id = gNodeId
if result.id == nodeIdToDebug:
Expand All @@ -1204,9 +1194,7 @@ proc newNodeI*(kind: TNodeKind, info: TLineInfo): PNode =
inc gNodeId

proc newNodeI*(kind: TNodeKind, info: TLineInfo, children: int): PNode =
new(result)
result.kind = kind
result.info = info
result = PNode(kind: kind, info: info)
if children > 0:
newSeq(result.sons, children)
when defined(useNodeIds):
Expand All @@ -1217,12 +1205,9 @@ proc newNodeI*(kind: TNodeKind, info: TLineInfo, children: int): PNode =
inc gNodeId

proc newNode*(kind: TNodeKind, info: TLineInfo, sons: TNodeSeq = @[],
typ: PType = nil): PNode =
new(result)
result.kind = kind
result.info = info
result.typ = typ
typ: PType = nil): PNode =
# XXX use shallowCopy here for ownership transfer:
result = PNode(kind: kind, info: info, typ: typ)
result.sons = sons
when defined(useNodeIds):
result.id = gNodeId
Expand Down Expand Up @@ -1321,14 +1306,10 @@ proc `$`*(s: PSym): string =
result = "<nil>"

proc newType*(kind: TTypeKind, owner: PSym): PType =
new(result)
result.kind = kind
result.owner = owner
result.size = -1
result.align = -1 # default alignment
result.id = getID()
result.uniqueId = result.id
result.lockLevel = UnspecifiedLockLevel
let id = getID()
result = PType(kind: kind, owner: owner, size: defaultSize,
align: defaultAlignment, id: id, uniqueId: id,
lockLevel: UnspecifiedLockLevel)
when debugIds:
registerId(result)
when false:
Expand Down Expand Up @@ -1543,6 +1524,51 @@ proc copyNode*(src: PNode): PNode =
of nkStrLit..nkTripleStrLit: result.strVal = src.strVal
else: discard

template transitionNodeKindCommon(k: TNodeKind) =
let obj {.inject.} = n[]
n[] = TNode(kind: k, typ: obj.typ, info: obj.info, flags: obj.flags,
comment: obj.comment)
when defined(useNodeIds):
n.id = obj.id

proc transitionSonsKind*(n: PNode, kind: range[nkComesFrom..nkTupleConstr]) =
transitionNodeKindCommon(kind)
n.sons = obj.sons

proc transitionIntKind*(n: PNode, kind: range[nkCharLit..nkUInt64Lit]) =
transitionNodeKindCommon(kind)
n.intVal = obj.intVal

proc transitionNoneToSym*(n: PNode) =
transitionNodeKindCommon(nkSym)

template transitionSymKindCommon*(k: TSymKind) =
let obj {.inject.} = s[]
s[] = TSym(kind: k, id: obj.id, magic: obj.magic, typ: obj.typ, name: obj.name,
info: obj.info, owner: obj.owner, flags: obj.flags, ast: obj.ast,
options: obj.options, position: obj.position, offset: obj.offset,
loc: obj.loc, annex: obj.annex, constraint: obj.constraint)
when hasFFI:
s.cname = obj.cname
when defined(nimsuggest):
s.allUsages = obj.allUsages

proc transitionGenericParamToType*(s: PSym) =
transitionSymKindCommon(skType)
s.typeInstCache = obj.typeInstCache

proc transitionRoutineSymKind*(s: PSym, kind: range[skProc..skTemplate]) =
transitionSymKindCommon(kind)
s.procInstCache = obj.procInstCache
s.gcUnsafetyReason = obj.gcUnsafetyReason
s.transformedBody = obj.transformedBody

proc transitionToLet*(s: PSym) =
transitionSymKindCommon(skLet)
s.guard = obj.guard
s.bitsize = obj.bitsize
s.alignment = obj.alignment

proc shallowCopy*(src: PNode): PNode =
# does not copy its sons, but provides space for them:
if src == nil: return nil
Expand Down
4 changes: 2 additions & 2 deletions compiler/canonicalizer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ proc encodeType(w: PRodWriter, t: PType, result: var string) =
result.add('+')
encodeVInt(t.id, result)
if t.n != nil:
encodeNode(w, unknownLineInfo(), t.n, result)
encodeNode(w, unknownLineInfo, t.n, result)
if t.flags != {}:
result.add('$')
encodeVInt(cast[int32](t.flags), result)
Expand Down Expand Up @@ -364,7 +364,7 @@ proc encodeSym(w: PRodWriter, s: PSym, result: var string) =
if s.annex != nil: encodeLib(w, s.annex, s.info, result)
if s.constraint != nil:
result.add('#')
encodeNode(w, unknownLineInfo(), s.constraint, result)
encodeNode(w, unknownLineInfo, s.constraint, result)
# lazy loading will soon reload the ast lazily, so the ast needs to be
# the last entry of a symbol:
if s.ast != nil:
Expand Down
12 changes: 6 additions & 6 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ proc genArrToSeq(p: BProc, n: PNode, d: var TLoc) =
genAssignment(p, elem, arr, {needToCopy})
else:
var i: TLoc
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo(), tyInt), i)
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt), i)
linefmt(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n", [i.r, L])
initLoc(elem, locExpr, lodeTyp elemType(skipTypes(n.typ, abstractInst)), OnHeap)
elem.r = ropecg(p.module, "$1$3[$2]", [rdLoc(d), rdLoc(i), dataField(p)])
Expand Down Expand Up @@ -1847,10 +1847,10 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
initLocExpr(p, e[1], a)
putIntoDest(p, d, e, ropecg(p.module, "#cardSet($1, $2)", [rdCharLoc(a), size]))
of mLtSet, mLeSet:
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo(), tyInt), i) # our counter
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt), i) # our counter
initLocExpr(p, e[1], a)
initLocExpr(p, e[2], b)
if d.k == locNone: getTemp(p, getSysType(p.module.g.graph, unknownLineInfo(), tyBool), d)
if d.k == locNone: getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyBool), d)
if op == mLtSet:
linefmt(p, cpsStmts, lookupOpr[mLtSet],
[rdLoc(i), size, rdLoc(d), rdLoc(a), rdLoc(b)])
Expand All @@ -1866,7 +1866,7 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
putIntoDest(p, d, e, ropecg(p.module, "(#nimCmpMem($1, $2, $3)==0)", [a.rdCharLoc, b.rdCharLoc, size]))
of mMulSet, mPlusSet, mMinusSet, mSymDiffSet:
# we inline the simple for loop for better code generation:
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo(), tyInt), i) # our counter
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt), i) # our counter
initLocExpr(p, e[1], a)
initLocExpr(p, e[2], b)
if d.k == locNone: getTemp(p, setType, d)
Expand Down Expand Up @@ -2319,7 +2319,7 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) =
[rdLoc(d), getTypeDesc(p.module, e.typ)])
for it in e.sons:
if it.kind == nkRange:
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo(), tyInt), idx) # our counter
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt), idx) # our counter
initLocExpr(p, it[0], a)
initLocExpr(p, it[1], b)
lineF(p, cpsStmts, "for ($1 = $3; $1 <= $4; $1++) $n" &
Expand All @@ -2335,7 +2335,7 @@ proc genSetConstr(p: BProc, e: PNode, d: var TLoc) =
lineF(p, cpsStmts, "$1 = 0;$n", [rdLoc(d)])
for it in e.sons:
if it.kind == nkRange:
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo(), tyInt), idx) # our counter
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt), idx) # our counter
initLocExpr(p, it[0], a)
initLocExpr(p, it[1], b)
lineF(p, cpsStmts, "for ($1 = $3; $1 <= $4; $1++) $n" &
Expand Down
2 changes: 1 addition & 1 deletion compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ proc genComputedGoto(p: BProc; n: PNode) =
let it = n[j]
if it.kind in {nkLetSection, nkVarSection}:
let asgn = copyNode(it)
asgn.kind = nkAsgn
asgn.transitionSonsKind(nkAsgn)
asgn.sons.setLen 2
for sym, value in it.fieldValuePairs:
if value.kind != nkEmpty:
Expand Down
4 changes: 2 additions & 2 deletions compiler/ccgtrav.nim
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ proc genTraverseProc(c: TTraversalClosure, accessor: Rope, typ: PType) =
of tyArray:
let arraySize = lengthOrd(c.p.config, typ[0])
var i: TLoc
getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo(), tyInt), i)
getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo, tyInt), i)
let oldCode = p.s(cpsStmts)
linefmt(p, cpsStmts, "for ($1 = 0; $1 < $2; $1++) {$n",
[i.r, arraySize])
Expand Down Expand Up @@ -119,7 +119,7 @@ proc genTraverseProcSeq(c: TTraversalClosure, accessor: Rope, typ: PType) =
var p = c.p
assert typ.kind == tySequence
var i: TLoc
getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo(), tyInt), i)
getTemp(p, getSysType(c.p.module.g.graph, unknownLineInfo, tyInt), i)
let oldCode = p.s(cpsStmts)
var a: TLoc
a.r = accessor
Expand Down
2 changes: 1 addition & 1 deletion compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ proc getTypeDescAux(m: BModule, origTyp: PType, check: var IntSet): Rope =
var etB = et.skipTypes(abstractInst)
if mapType(m.config, t) == ctPtrToArray:
if etB.kind == tySet:
et = getSysType(m.g.graph, unknownLineInfo(), tyUInt8)
et = getSysType(m.g.graph, unknownLineInfo, tyUInt8)
else:
et = elemType(etB)
etB = et.skipTypes(abstractInst)
Expand Down
2 changes: 1 addition & 1 deletion compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ proc getIntTemp(p: BProc, result: var TLoc) =
linefmt(p, cpsLocals, "NI $1;$n", [result.r])
result.k = locTemp
result.storage = OnStack
result.lode = lodeTyp getSysType(p.module.g.graph, unknownLineInfo(), tyInt)
result.lode = lodeTyp getSysType(p.module.g.graph, unknownLineInfo, tyInt)
result.flags = {}

proc localVarDecl(p: BProc; n: PNode): Rope =
Expand Down
6 changes: 3 additions & 3 deletions compiler/cgmeth.nim
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ proc genDispatcher(g: ModuleGraph; methods: seq[PSym], relevantCols: IntSet): PS
var paramLen = base.typ.len
var nilchecks = newNodeI(nkStmtList, base.info)
var disp = newNodeI(nkIfStmt, base.info)
var ands = getSysMagic(g, unknownLineInfo(), "and", mAnd)
var iss = getSysMagic(g, unknownLineInfo(), "of", mOf)
let boolType = getSysType(g, unknownLineInfo(), tyBool)
var ands = getSysMagic(g, unknownLineInfo, "and", mAnd)
var iss = getSysMagic(g, unknownLineInfo, "of", mOf)
let boolType = getSysType(g, unknownLineInfo, tyBool)
for col in 1..<paramLen:
if contains(relevantCols, col):
let param = base.typ.n[col].sym
Expand Down
2 changes: 1 addition & 1 deletion compiler/closureiters.nim
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ proc lowerStmtListExprs(ctx: var Ctx, n: PNode, needsSplit: var bool): PNode =
result = newNodeI(nkStmtListExpr, n.info)
result.typ = n.typ
let (st, ex) = exprToStmtList(n[1])
n.kind = nkBlockStmt
n.transitionSonsKind(nkBlockStmt)
n.typ = nil
n[1] = st
result.add(n)
Expand Down
6 changes: 3 additions & 3 deletions compiler/lambdalifting.nim
Original file line number Diff line number Diff line change
Expand Up @@ -837,12 +837,12 @@ proc liftIterToProc*(g: ModuleGraph; fn: PSym; body: PNode; ptrType: PType): PNo
# pretend 'fn' is a closure iterator for the analysis:
let oldKind = fn.kind
let oldCC = fn.typ.callConv
fn.kind = skIterator
fn.transitionRoutineSymKind(skIterator)
fn.typ.callConv = ccClosure
d.ownerToType[fn.id] = ptrType
detectCapturedVars(body, fn, d)
result = liftCapturedVars(body, fn, d, c)
fn.kind = oldKind
fn.transitionRoutineSymKind(oldKind)
fn.typ.callConv = oldCC

proc liftLambdas*(g: ModuleGraph; fn: PSym, body: PNode; tooEarly: var bool): PNode =
Expand Down Expand Up @@ -959,7 +959,7 @@ proc liftForLoop*(g: ModuleGraph; body: PNode; owner: PSym): PNode =
var vpart = newNodeI(if body.len == 3: nkIdentDefs else: nkVarTuple, body.info)
for i in 0..<body.len-2:
if body[i].kind == nkSym:
body[i].sym.kind = skLet
body[i].sym.transitionToLet()
vpart.add body[i]

vpart.add newNodeI(nkEmpty, body.info) # no explicit type
Expand Down
8 changes: 2 additions & 6 deletions compiler/lineinfos.nim
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,7 @@ proc raiseRecoverableError*(msg: string) {.noinline.} =

const
InvalidFileIdx* = FileIndex(-1)

proc unknownLineInfo*(): TLineInfo =
result.line = uint16(0)
result.col = int16(-1)
result.fileIndex = InvalidFileIdx
unknownLineInfo* = TLineInfo(line: 0, col: -1, fileIndex: InvalidFileIdx)

type
Severity* {.pure.} = enum ## VS Code only supports these three
Expand All @@ -267,7 +263,7 @@ type

proc initMsgConfig*(): MsgConfig =
result.msgContext = @[]
result.lastError = unknownLineInfo()
result.lastError = unknownLineInfo
result.filenameToIndexTbl = initTable[string, FileIndex]()
result.fileInfos = @[]
result.errorOutputs = {eStdOut, eStdErr}
2 changes: 1 addition & 1 deletion compiler/lowerings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ proc genDeref*(n: PNode; k = nkHiddenDeref): PNode =
result.add n

proc callCodegenProc*(g: ModuleGraph; name: string;
info: TLineInfo = unknownLineInfo();
info: TLineInfo = unknownLineInfo;
arg1, arg2, arg3, optionalArgs: PNode = nil): PNode =
result = newNodeI(nkCall, info)
let sym = magicsys.getCompilerProc(g, name)
Expand Down
2 changes: 1 addition & 1 deletion compiler/modulegraphs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ proc stopCompile*(g: ModuleGraph): bool {.inline.} =
result = g.doStopCompile != nil and g.doStopCompile()

proc createMagic*(g: ModuleGraph; name: string, m: TMagic): PSym =
result = newSym(skProc, getIdent(g.cache, name), nil, unknownLineInfo(), {})
result = newSym(skProc, getIdent(g.cache, name), nil, unknownLineInfo, {})
result.magic = m

proc newModuleGraph*(cache: IdentCache; config: ConfigRef): ModuleGraph =
Expand Down
10 changes: 4 additions & 6 deletions compiler/modules.nim
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ proc partialInitModule(result: PSym; graph: ModuleGraph; fileIdx: FileIndex; fil
strTableAdd(packSym.tab, result)

proc newModule(graph: ModuleGraph; fileIdx: FileIndex): PSym =
let filename = AbsoluteFile toFullPath(graph.config, fileIdx)
# We cannot call ``newSym`` here, because we have to circumvent the ID
# mechanism, which we do in order to assign each module a persistent ID.
new(result)
result.id = -1 # for better error checking
result.kind = skModule
let filename = AbsoluteFile toFullPath(graph.config, fileIdx)
result.name = getIdent(graph.cache, splitFile(filename).name)
result = PSym(kind: skModule, id: -1, # for better error checking
name: getIdent(graph.cache, splitFile(filename).name),
info: newLineInfo(fileIdx, 1, 1))
if not isNimIdentifier(result.name.s):
rawMessage(graph.config, errGenerated, "invalid module name: " & result.name.s)
result.info = newLineInfo(fileIdx, 1, 1)
partialInitModule(result, graph, fileIdx, filename)

proc compileModule*(graph: ModuleGraph; fileIdx: FileIndex; flags: TSymFlags): PSym =
Expand Down
Loading

0 comments on commit 796aafe

Please sign in to comment.