Skip to content

Commit 5d9b640

Browse files
committed
jsgen: use MIR type IDs for type info names
Summary ======= Use the MIR types IDs for the names of type-info variables emitted by the JS code generator, instead of `PType` IDs. Details ======= Beyond being a step towards phasing out usage of `PType` in the code generators, in the interim, this allows for emitting type info for `PType`s without a proper ID (e.g., types generating using the module graph's ID generator). Such improper IDs can be negative values, which would lead in invalid JavaScript identifiers.
1 parent ce529cb commit 5d9b640

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

compiler/backend/jsgen.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ import
4545
],
4646
compiler/mir/[
4747
mirenv,
48-
mirtrees
48+
mirtrees,
49+
mirtypes
4950
],
5051
compiler/front/[
5152
options,

compiler/backend/jstypes.nim

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ proc genObjectInfo(p: PProc, typ: PType, name: Rope) =
7777
var s = ("var $1 = {size: 0, kind: $2, base: null, node: null, " &
7878
"finalizer: null};$n") % [name, rope(ord(kind))]
7979
prepend(p.g.typeInfo, s)
80+
let tid = ord(p.g.env.types.add(typ))
8081
p.g.typeInfo.addf("var NNI$1 = $2;$n",
81-
[rope(typ.id), genObjectFields(p, typ, typ.n)])
82-
p.g.typeInfo.addf("$1.node = NNI$2;$n", [name, rope(typ.id)])
82+
[$tid, genObjectFields(p, typ, typ.n)])
83+
p.g.typeInfo.addf("$1.node = NNI$2;$n", [name, $tid])
8384
if (typ.kind == tyObject) and (typ[0] != nil):
8485
p.g.typeInfo.addf("$1.base = $2;$n",
8586
[name, genTypeInfo(p, typ[0].skipTypes(skipPtrs))])
@@ -98,9 +99,10 @@ proc genTupleInfo(p: PProc, typ: PType, name: Rope) =
9899
var s = ("var $1 = {size: 0, kind: $2, base: null, node: null, " &
99100
"finalizer: null};$n") % [name, rope(ord(typ.kind))]
100101
prepend(p.g.typeInfo, s)
102+
let tid = ord(p.g.env.types.add(typ))
101103
p.g.typeInfo.addf("var NNI$1 = $2;$n",
102-
[rope(typ.id), genTupleFields(p, typ)])
103-
p.g.typeInfo.addf("$1.node = NNI$2;$n", [name, rope(typ.id)])
104+
[$tid, genTupleFields(p, typ)])
105+
p.g.typeInfo.addf("$1.node = NNI$2;$n", [name, $tid])
104106

105107
proc genEnumInfo(p: PProc, typ: PType, name: Rope) =
106108
var s = ""
@@ -111,21 +113,23 @@ proc genEnumInfo(p: PProc, typ: PType, name: Rope) =
111113
let extName = if field.ast == nil: field.name.s else: field.ast.strVal
112114
s.addf("\"$1\": {kind: 1, offset: $1, typ: $2, name: $3, len: 0, sons: null}",
113115
[rope(field.position), name, makeJSString(extName)])
116+
let tid = ord(p.g.env.types.add(typ))
114117
var n = ("var NNI$1 = {kind: 2, offset: 0, typ: null, " &
115-
"name: null, len: $2, sons: {$3}};$n") % [rope(typ.id), rope(typ.n.len), s]
118+
"name: null, len: $2, sons: {$3}};$n") % [$tid, rope(typ.n.len), s]
116119
s = ("var $1 = {size: 0, kind: $2, base: null, node: null, " &
117120
"finalizer: null};$n") % [name, rope(ord(typ.kind))]
118121
prepend(p.g.typeInfo, s)
119122
p.g.typeInfo.add(n)
120-
p.g.typeInfo.addf("$1.node = NNI$2;$n", [name, rope(typ.id)])
123+
p.g.typeInfo.addf("$1.node = NNI$2;$n", [name, $tid])
121124
if typ[0] != nil:
122125
p.g.typeInfo.addf("$1.base = $2;$n",
123126
[name, genTypeInfo(p, typ[0])])
124127

125128
proc genTypeInfo(p: PProc, typ: PType): Rope =
126129
let t = typ.skipTypes({tyGenericInst, tyDistinct, tyAlias, tySink})
127-
result = "NTI$1" % [rope(t.id)]
128-
if containsOrIncl(p.g.typeInfoGenerated, t.id): return
130+
let id = ord(p.g.env.types.add(t))
131+
result = "NTI" & $id
132+
if containsOrIncl(p.g.typeInfoGenerated, id): return
129133
case t.kind
130134
of tyDistinct:
131135
result = genTypeInfo(p, t[0])

0 commit comments

Comments
 (0)