Skip to content

Commit edab1c2

Browse files
committed
assigns PNode.typ
1 parent 6985b4f commit edab1c2

File tree

4 files changed

+79
-42
lines changed

4 files changed

+79
-42
lines changed

compiler/icnif/nifdecoder.nim

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ proc fromNifNodeFlags(n: var Cursor): set[TNodeFlag] =
125125
inc n
126126
skipParRi n
127127

128+
proc fromNifIntLit(c: var DecodeContext; n: var Cursor): PNode =
129+
# See `getIntLitType` proc in semdata.nim
130+
# Use intTypeCache?
131+
let sysType = c.getSysType(tyInt)
132+
var typ = copyType(sysType, c.idgen, sysType.owner)
133+
let val = pool.integers[n.intId]
134+
inc n
135+
result = newIntTypeNode(val, typ)
136+
typ.n = newIntTypeNode(val, typ)
137+
typ.n.flags = {nfSem}
138+
128139
proc fromNifLocal(c: var DecodeContext; n: var Cursor; kind: TNodeKind): PNode =
129140
result = newNodeI(kind, unknownLineInfo, 1)
130141
inc n
@@ -137,7 +148,9 @@ proc fromNifLocal(c: var DecodeContext; n: var Cursor; kind: TNodeKind): PNode =
137148
if n.kind == DotToken:
138149
inc n
139150
else:
140-
result[0][0].sym.typ = fromNifType(c, n)
151+
let typ = fromNifType(c, n)
152+
result[0][0].sym.typ = typ
153+
result[0][0].typ = typ
141154
result[0][2] = fromNif(c, n)
142155
skipParRi n # nkIdentDefs
143156
skipParRi n
@@ -165,7 +178,9 @@ proc fromNifTypeSection(c: var DecodeContext; n: var Cursor): PNode =
165178

166179
# type body
167180
result[2] = newNode(nkEmpty)
168-
sym.sym.typ = fromNifType(c, n)
181+
let typ = fromNifType(c, n)
182+
sym.sym.typ = typ
183+
sym.typ = typ
169184

170185
skipParRi n
171186

@@ -192,6 +207,7 @@ proc fromNifSuf(c: var DecodeContext; n: var Cursor): PNode =
192207
assert false, "Unknown string literal suffix " & suffix
193208
nkNone
194209
result = newStrNode(kind, v)
210+
result.typ = c.getSysType(tyString)
195211
of IntLit:
196212
let v = pool.integers[n.intId]
197213
incExpect n, StringLit
@@ -234,7 +250,7 @@ proc fromNifSuf(c: var DecodeContext; n: var Cursor): PNode =
234250
of "f32":
235251
(nkFloat32Lit, tyFloat32)
236252
of "f64":
237-
(nkFloat64Lit, tyFloat64)
253+
(nkFloat64Lit, tyFloat)
238254
of "f128":
239255
(nkFloat128Lit, tyFloat128)
240256
else:
@@ -257,13 +273,14 @@ proc fromNif(c: var DecodeContext; n: var Cursor): PNode =
257273
result = newSymNode(fromNifSymbol(c, n))
258274
of StringLit:
259275
result = newStrNode(nkStrLit, pool.strings[n.litId])
276+
result.typ = c.getSysType(tyString)
260277
inc n
261278
of CharLit:
262279
result = newIntNode(nkCharLit, n.charLit.int)
280+
result.typ = c.getSysType(tyChar)
263281
inc n
264282
of IntLit:
265-
result = newIntTypeNode(pool.integers[n.intId], c.getSysType(tyInt))
266-
inc n
283+
result = fromNifIntLit(c, n)
267284
of UIntLit:
268285
result = newIntTypeNode(cast[BiggestInt](pool.uintegers[n.uintId]), c.getSysType(tyUInt))
269286
inc n

compiler/icnif/nifdecodertypes.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ proc fromNifTypeImpl(c: var DecodeContext; n: var Cursor; kind: TTypeKind; res:
7373
while n.kind != ParRi:
7474
var sym = fromNifSymDef(c, n, nkEnumTy)
7575
sym.sym.typ = res
76+
sym.typ = res
7677
res.n.add sym
7778
inc n
7879
res.addAllowNil nil
@@ -92,7 +93,9 @@ proc fromNifTypeImpl(c: var DecodeContext; n: var Cursor; kind: TTypeKind; res:
9293
while n.kind != ParRi:
9394
var sym = fromNifSymDef(c, n, nkRecList)
9495
res.n.add sym
95-
sym.sym.typ = fromNifType(c, n)
96+
let typ = fromNifType(c, n)
97+
sym.sym.typ = typ
98+
sym.typ = typ
9699
inc n
97100
else:
98101
while n.kind != ParRi:

compiler/icnif/nifencodertypes.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ proc toNif(c: var EncodeContext; t: PType; isTypeSection = false) =
4444
of tyUInt16: atom c, t, "u +16"
4545
of tyUInt32: atom c, t, "u +32"
4646
of tyUInt64: atom c, t, "u +64"
47-
of tyFloat, tyFloat64: atom c, t, "f -1"
47+
of tyFloat: atom c, t, "f -1"
4848
of tyFloat32: atom c, t, "f +32"
49+
of tyFloat64: atom c, t, "f +64"
4950
of tyFloat128: atom c, t, "f +128"
5051
of tyAlias:
5152
c.typeHead t:

tests/icnif/tencode_node2node.nim

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type
6565
nodeStack: seq[PNode]
6666
#symStack: seq[PSym]
6767
typStack: seq[PType]
68+
conf: ConfigRef # used to print the line info when there is a mismatch.
6869

6970
# Compare PType, PSym and PNode but ignores fields nifencoder and nifdecoder doesn't support
7071
# Assumes `x` is generated by sem.nim and `y` is decoded by icnif/nifdecoder.
@@ -92,6 +93,8 @@ proc eql(x, y: PType; c: var EqlContext): bool =
9293
c.typStack.add x
9394
if not eql(x.n, y.n, c):
9495
echo "type.n mismatch"
96+
debug(x.n)
97+
debug(y.n)
9598
result = false
9699
elif x.kidsLen != y.kidsLen:
97100
echo "type kidsLen mismatch"
@@ -145,6 +148,8 @@ proc eql(x, y: PNode; c: var EqlContext): bool =
145148
result = false
146149
elif x.flags != y.flags:
147150
echo "node flag mismatch: ", x.flags, "/", y.flags
151+
debug(x)
152+
debug(y)
148153
result = false
149154
elif x.safeLen == y.safeLen:
150155
if c.nodeStack.len != 0:
@@ -153,43 +158,53 @@ proc eql(x, y: PNode; c: var EqlContext): bool =
153158
# echo "cycle is detected in PNode"
154159
return true
155160
c.nodeStack.add x
156-
case x.kind:
157-
of nkSym:
158-
result = eql(x.sym, y.sym, c)
159-
if not result:
160-
echo "Symbol mismatch:"
161-
debug(x.sym)
162-
debug(y.sym)
163-
debug(x.sym.typ)
164-
debug(y.sym.typ)
165-
of nkCharLit .. nkTripleStrLit:
166-
result = sameValue(x, y)
167-
of nkIdentDefs:
168-
assert x.len == 3 and y.len == 3
169-
if eql(x[0], y[0], c) and eql(x[2], y[2], c) and y[1].kind == nkEmpty and y[0].sym.typ != nil:
170-
result = true
171-
else:
172-
echo "nkIdentDefs mismatch"
173-
result = false
174-
of nkTypeDef:
175-
assert x.len == 3 and y.len == 3
176-
if eql(x[0], y[0], c) and eql(x[1], y[1], c):
177-
let sym = if y[0].kind == nkPostfix:
178-
y[0][1].sym
179-
else:
180-
y[0].sym
181-
result = y[2].kind == nkEmpty and sym.typ != nil
182-
else:
183-
result = false
184-
if not result:
185-
echo "nkTypeDef mismatch"
161+
if x.kind != nkNilLit and not eql(x.typ, y.typ, c):
162+
# ignore node type if it is nil literal.
163+
# current nifencoder doesn't encode the type of nil lit.
164+
echo "PNode type mismatch at ", `$`(c.conf, x.info), ":"
165+
debug(x)
166+
debug(y)
167+
debug(x.typ)
168+
debug(y.typ)
169+
result = false
186170
else:
187-
result = true
188-
for i in 0 ..< x.safeLen:
189-
if not eql(x[i], y[i], c):
171+
case x.kind:
172+
of nkSym:
173+
result = eql(x.sym, y.sym, c)
174+
if not result:
175+
echo "Symbol mismatch:"
176+
debug(x.sym)
177+
debug(y.sym)
178+
debug(x.sym.typ)
179+
debug(y.sym.typ)
180+
of nkCharLit .. nkTripleStrLit:
181+
result = sameValue(x, y)
182+
of nkIdentDefs:
183+
assert x.len == 3 and y.len == 3
184+
if eql(x[0], y[0], c) and eql(x[2], y[2], c) and y[1].kind == nkEmpty and y[0].sym.typ != nil:
185+
result = true
186+
else:
187+
echo "nkIdentDefs mismatch"
190188
result = false
191-
break
192-
discard c.nodeStack.pop
189+
of nkTypeDef:
190+
assert x.len == 3 and y.len == 3
191+
if eql(x[0], y[0], c) and eql(x[1], y[1], c):
192+
let sym = if y[0].kind == nkPostfix:
193+
y[0][1].sym
194+
else:
195+
y[0].sym
196+
result = y[2].kind == nkEmpty and sym.typ != nil
197+
else:
198+
result = false
199+
if not result:
200+
echo "nkTypeDef mismatch"
201+
else:
202+
result = true
203+
for i in 0 ..< x.safeLen:
204+
if not eql(x[i], y[i], c):
205+
result = false
206+
break
207+
discard c.nodeStack.pop
193208
else:
194209
echo "node length mismatch"
195210
debug(x)
@@ -209,6 +224,7 @@ proc testNifEncDec(graph: ModuleGraph; src: string) =
209224
let n2 = loadNifFromBuffer(nif, fullPath, graphForLoad)
210225
#debug(n2)
211226
var c = EqlContext()
227+
c.conf = graph.config
212228
assert eql(n, n2, c)
213229

214230
var conf = newConfigRefForTest()

0 commit comments

Comments
 (0)