Skip to content

Commit 96ff1a8

Browse files
committed
saves/loads PNode and PSym line info
1 parent b5e541b commit 96ff1a8

File tree

3 files changed

+75
-28
lines changed

3 files changed

+75
-28
lines changed

compiler/icnif/nifdecoder.nim

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import std / [assertions, tables]
2-
import "../../dist/nimony/src/lib" / [bitabs, nifreader, nifstreams, nifcursors]
2+
import "../../dist/nimony/src/lib" / [bitabs, nifreader, nifstreams, nifcursors, lineinfos]
33
import ".." / [ast, idents, lineinfos, options, modules, modulegraphs, msgs, pathutils]
44
import enum2nif, icniftags
55

@@ -59,6 +59,13 @@ when false:
5959
else:
6060
expectTag(n, id)
6161

62+
proc fromNifLineInfo(c: var DecodeContext; n: Cursor): TLineInfo =
63+
if n.info == NoLineInfo:
64+
unknownLineInfo
65+
else:
66+
let info = pool.man.unpack(n.info)
67+
c.graph.config.newLineInfo(pool.files[info.file].AbsoluteFile, info.line, info.col)
68+
6269
proc fromNifModuleId(c: var DecodeContext; n: var Cursor): (FileIndex, int32) =
6370
expect n, {ParLe, IntLit}
6471
if n.kind == ParLe:
@@ -83,6 +90,7 @@ proc fromNif(c: var DecodeContext; n: var Cursor): PNode
8390

8491
proc fromNifSymDef(c: var DecodeContext; n: var Cursor): PSym =
8592
expectTag n, symIdTag
93+
let info = c.fromNifLineInfo n
8694
inc n
8795
let (itemIdModule, nifModId) = c.fromNifModuleId(n)
8896
expect n, IntLit
@@ -100,6 +108,7 @@ proc fromNifSymDef(c: var DecodeContext; n: var Cursor): PSym =
100108
result = PSym(itemId: ItemId(module: itemIdModule.int32, item: itemId),
101109
kind: kind,
102110
name: ident,
111+
info: info,
103112
flags: flags,
104113
disamb: disamb)
105114

@@ -223,9 +232,10 @@ proc fromNifType(c: var DecodeContext; n: var Cursor): PType =
223232
assert false, "expected type tag but got " & pool.tags[n.tagId]
224233

225234
template withNode(c: var DecodeContext; n: var Cursor; result: PNode; kind: TNodeKind; body: untyped) =
235+
let info = c.fromNifLineInfo(n)
226236
incExpect n, {DotToken, Ident}
227237
let flags = fromNifNodeFlags n
228-
result = newNode(kind)
238+
result = newNodeI(kind, info)
229239
result.flags = flags
230240
result.typ = c.fromNifType n
231241
body
@@ -241,12 +251,12 @@ proc fromNif(c: var DecodeContext; n: var Cursor): PNode =
241251
let kind = n.nodeKind
242252
case kind:
243253
of nkEmpty:
244-
result = newNode(nkEmpty)
254+
result = newNodeI(nkEmpty, c.fromNifLineInfo(n))
245255
inc n
246256
skipParRi n
247257
of nkIdent:
248258
incExpect n, Ident
249-
result = newIdentNode(c.graph.cache.getIdent(pool.strings[n.litId]), unknownLineInfo)
259+
result = newIdentNode(c.graph.cache.getIdent(pool.strings[n.litId]), c.fromNifLineInfo(n))
250260
inc n
251261
skipParRi n
252262
of nkSym:

compiler/icnif/nifencoder.nim

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ proc writeFlags[E](dest: var TokenBuf; flags: set[E]) =
3131
else:
3232
dest.addDotToken
3333

34+
proc toNif(c: var EncodeContext; info: TLineInfo): PackedLineInfo =
35+
if info == unknownLineInfo:
36+
NoLineInfo
37+
else:
38+
let fileId = pool.files.getOrIncl(c.conf.toFullPath(info.fileIndex))
39+
pack(pool.man, fileId, info.line.int32, info.col)
40+
3441
proc toNifModuleId(c: var EncodeContext; moduleId: int) =
3542
# `ItemId.module` in PType and PSym (and `PSym.position` when it is skModule) are module's FileIndex
3643
# but it cannot be directly encoded as the uniqueness of it can broke
@@ -48,28 +55,29 @@ proc toNif(c: var EncodeContext; typ: PType)
4855
proc toNif(c: var EncodeContext; n: PNode)
4956

5057
proc toNifDef(c: var EncodeContext; sym: PSym) =
51-
c.dest.buildTree symIdTag:
52-
c.toNifModuleId sym.itemId.module
53-
c.dest.addIntLit sym.itemId.item
54-
c.dest.addIdent sym.name.s
55-
c.dest.writeFlags sym.flags
56-
c.dest.addIntLit sym.disamb
57-
c.dest.buildTree sym.kind.toNifTag:
58-
case sym.kind
59-
of skLet, skVar, skField, skForVar:
60-
c.toNif sym.guard
61-
c.dest.addIntLit sym.bitsize
62-
c.dest.addIntLit sym.alignment
63-
else:
64-
discard
65-
if sym.kind == skModule:
66-
c.toNifModuleId sym.position
58+
c.dest.addParLe symIdTag, c.toNif sym.info
59+
c.toNifModuleId sym.itemId.module
60+
c.dest.addIntLit sym.itemId.item
61+
c.dest.addIdent sym.name.s
62+
c.dest.writeFlags sym.flags
63+
c.dest.addIntLit sym.disamb
64+
c.dest.buildTree sym.kind.toNifTag:
65+
case sym.kind
66+
of skLet, skVar, skField, skForVar:
67+
c.toNif sym.guard
68+
c.dest.addIntLit sym.bitsize
69+
c.dest.addIntLit sym.alignment
6770
else:
68-
c.dest.addIntLit sym.position
69-
c.toNif sym.typ
70-
c.toNif sym.owner
71-
c.dest.addIdent toNifTag(sym.loc.k)
72-
c.dest.addStrLit sym.loc.snippet
71+
discard
72+
if sym.kind == skModule:
73+
c.toNifModuleId sym.position
74+
else:
75+
c.dest.addIntLit sym.position
76+
c.toNif sym.typ
77+
c.toNif sym.owner
78+
c.dest.addIdent toNifTag(sym.loc.k)
79+
c.dest.addStrLit sym.loc.snippet
80+
c.dest.addParRi
7381

7482
proc toNifDef(c: var EncodeContext; typ: PType) =
7583
c.dest.buildTree typeIdTag:
@@ -115,7 +123,7 @@ proc writeNodeFlags(dest: var TokenBuf; flags: set[TNodeFlag]) {.inline.} =
115123
writeFlags dest, flags
116124

117125
template withNode(c: var EncodeContext; n: PNode; body: untyped) =
118-
c.dest.addParLe pool.tags.getOrIncl(toNifTag(n.kind))
126+
c.dest.addParLe pool.tags.getOrIncl(toNifTag(n.kind)), c.toNif n.info
119127
writeNodeFlags(c.dest, n.flags)
120128
c.toNif n.typ
121129
body
@@ -127,10 +135,12 @@ proc toNif(c: var EncodeContext; n: PNode) =
127135
else:
128136
case n.kind:
129137
of nkEmpty:
130-
c.dest.addParLe pool.tags.getOrIncl(toNifTag(nkEmpty))
138+
let info = c.toNif n.info
139+
c.dest.addParLe pool.tags.getOrIncl(toNifTag(nkEmpty)), info
131140
c.dest.addParRi
132141
of nkIdent:
133-
c.dest.addParLe pool.tags.getOrIncl(toNifTag(nkIdent))
142+
let info = c.toNif n.info
143+
c.dest.addParLe pool.tags.getOrIncl(toNifTag(nkIdent)), info
134144
c.dest.addIdent n.ident.s
135145
c.dest.addParRi
136146
of nkSym:

tests/icnif/tencode_node2node.nim

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ proc eqlFileIndex(x, y: int; c: EqlContext): bool =
9090
else:
9191
result = true
9292

93+
proc eql(x, y: TLineInfo; c: EqlContext): bool =
94+
# If parent PNode has a valid line info but it's child doesn't have one,
95+
# cannot translate such a tree to NIF.
96+
# Because in NIF, if a child node doesn't have line info,
97+
# nifstream assign the parent's line info to it.
98+
# So cannot have child node without line info if parent has a valid line info.
99+
if x == unknownLineInfo:
100+
result = true
101+
elif x.line != y.line:
102+
echo "line number mismatch: ", x.line, "/", y.line
103+
result = false
104+
elif x.col != y.col:
105+
echo "column number mismatch: ", x.col, "/", y.col
106+
result = false
107+
elif not eqlFileIndex(x.fileIndex.int, y.fileIndex.int, c):
108+
echo "file in line info mismatch"
109+
result = false
110+
else:
111+
result = true
112+
93113
proc eqlSymPos(x, y: PSym; c: EqlContext): bool =
94114
if x.kind == skModule:
95115
result = eqlFileIndex(x.position, y.position, c)
@@ -123,6 +143,9 @@ proc eql(x, y: PSym; c: var EqlContext): bool =
123143
elif x.kind != y.kind:
124144
echo "symbol kind mismatch: ", x.kind, "/", y.kind
125145
result = false
146+
elif not eql(x.info, y.info, c):
147+
echo "symbol line info mismatch"
148+
result = false
126149
elif x.flags != y.flags:
127150
echo "symbol flag mismatch: ", x.flags, "/", y.flags
128151
result = false
@@ -229,6 +252,10 @@ proc eql(x, y: PNode; c: var EqlContext): bool =
229252
debug(x)
230253
debug(y)
231254
result = false
255+
elif not eql(x.info, y. info, c):
256+
echo "node lineinfo mismatch at ", `$`(c.confX, x.info)
257+
debug(x)
258+
result = false
232259
elif x.safeLen == y.safeLen:
233260
if c.nodeStack.len != 0:
234261
for i in countDown(c.nodeStack.len - 1, 0):

0 commit comments

Comments
 (0)