Skip to content

Commit

Permalink
internal: remove obsolete nkExprColonExpr detection
Browse files Browse the repository at this point in the history
Summary
=======

Remove detection of `nkExprColonExpr` nodes in the context of tuple,
array, and object construction expressions in the code generators. The
ASTs reaching the code generators are expected to have a specific shape;
violations thereof need to be treated as internal errors and should
not be silently supported.

Details
=======

The AST coming out of `astgen` does not contain `nkExprColonExpr` nodes
in `nkTupleConstr` trees, so the detection for them in
`ccgexprs.genTupleConstr` and `vmgen.genTupleConstr` is obsolete and
thus removed. Since `jsgen.genTupleConstr` is used for both normal
procedure-level code and constant initializers, it cannot have the
detection removed, as constant initializers do use `nkExprColonExpr`
nodes for named tuple constructions (`nkTupleConstr`).

In addition, the constant initializer AST that comes out of both
`semfold` or `vmcompilerserdes`:
- doesn't have `nkExprColonExpr` nodes in `nkBracket` (array
  construction) trees
- always has `nkExprColonExpr` nodes in `nkObjConstr` trees

Therefore, `ccgexprs.getNullValueAux` (code generation for constant
object constructions) can depend on the presence of `nkExprColonExpr`
nodes and `ccgexprs.genConstSimpleList` (code generation for constant
array constructions) on their absence.
  • Loading branch information
zerbina committed Jul 20, 2023
1 parent 99d1174 commit fee5a66
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
25 changes: 8 additions & 17 deletions compiler/backend/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1995,9 +1995,7 @@ proc genTupleConstr(p: BProc, n: PNode, d: var TLoc) =
let t = n.typ
discard getTypeDesc(p.module, t) # so that any fields are initialized
if d.k == locNone: getTemp(p, t, d)
for i in 0..<n.len:
var it = n[i]
if it.kind == nkExprColonExpr: it = it[1]
for i, it in n.pairs:
initLoc(rec, locExpr, it, d.storage)
rec.r = "$1.Field$2" % [rdLoc(d), rope(i)]
rec.flags.incl(lfEnforceDeref)
Expand Down Expand Up @@ -2373,12 +2371,9 @@ proc getNullValueAux(p: BProc; t: PType; obj, constOrNil: PNode,
if constOrNil != nil:
## find kind value, default is zero if not specified
for i in 1..<constOrNil.safeLen:
if constOrNil[i].kind == nkExprColonExpr:
if constOrNil[i][0].sym.name.id == obj[0].sym.name.id:
branch = getOrdValue(constOrNil[i][1])
break
elif i == obj[0].sym.position:
branch = getOrdValue(constOrNil[i])
assert constOrNil[i].kind == nkExprColonExpr
if constOrNil[i][0].sym.name.id == obj[0].sym.name.id:
branch = getOrdValue(constOrNil[i][1])
break

let selectedBranch = caseObjDefaultBranch(obj, branch)
Expand All @@ -2401,12 +2396,9 @@ proc getNullValueAux(p: BProc; t: PType; obj, constOrNil: PNode,
let field = obj.sym
if constOrNil != nil:
for i in 1..<constOrNil.safeLen:
if constOrNil[i].kind == nkExprColonExpr:
if constOrNil[i][0].sym.name.id == field.name.id:
result.add genBracedInit(p, constOrNil[i][1], isConst, field.typ)
return
elif i == field.position:
result.add genBracedInit(p, constOrNil[i], isConst, field.typ)
assert constOrNil[i].kind == nkExprColonExpr
if constOrNil[i][0].sym.name.id == field.name.id:
result.add genBracedInit(p, constOrNil[i][1], isConst, field.typ)
return
# not found, produce default value:
result.add getDefaultValue(p, field.typ, info)
Expand Down Expand Up @@ -2444,8 +2436,7 @@ proc genConstSimpleList(p: BProc, n: PNode; isConst: bool): Rope =
for i in 0..<n.len:
let it = n[i]
if i > 0: result.add ",\n"
if it.kind == nkExprColonExpr: result.add genBracedInit(p, it[1], isConst, it[0].typ)
else: result.add genBracedInit(p, it, isConst, it.typ)
result.add genBracedInit(p, it, isConst, it.typ)
result.add("}\n")

proc genConstTuple(p: BProc, n: PNode; isConst: bool; tup: PType): Rope =
Expand Down
15 changes: 4 additions & 11 deletions compiler/vm/vmgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2957,17 +2957,10 @@ proc genTupleConstr(c: var TCtx, n: PNode, dest: var TDest) =
if n.typ.kind != tyTypeDesc:
c.gABx(n, opcLdNull, dest, c.genType(n.typ))
# XXX x = (x.old, 22) produces wrong code ... stupid self assignments
for i in 0..<n.len:
let it = n[i]
if it.kind == nkExprColonExpr:
let idx = genField(c, it[0])
let tmp = c.genAsgnSource(it[1], wantsPtr = true)
c.gABC(it[1], opcWrObj, dest, idx, tmp)
c.freeTemp(tmp)
else:
let tmp = c.genAsgnSource(it, wantsPtr = true)
c.gABC(it, opcWrObj, dest, i.TRegister, tmp)
c.freeTemp(tmp)
for i, it in n.pairs:
let tmp = c.genAsgnSource(it, wantsPtr = true)
c.gABC(it, opcWrObj, dest, i.TRegister, tmp)
c.freeTemp(tmp)

proc genClosureConstr(c: var TCtx, n: PNode, dest: var TDest) =
if dest.isUnset: dest = c.getTemp(n.typ)
Expand Down

0 comments on commit fee5a66

Please sign in to comment.