Skip to content

Commit

Permalink
prevent codegen instead
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Nov 15, 2024
1 parent 1fc3c45 commit 304f51e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
4 changes: 4 additions & 0 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,10 @@ proc genObjConstr(p: BProc, e: PNode, d: var TLoc) =
discard getTypeDesc(p.module, t)
let ty = getUniqueType(t)
for i in 1..<e.len:
if nfPreventCg in e[i].flags:
# this is an object constructor node generated by the VM and
# this field is in an inactive case branch, don't generate assignment
continue
var check: PNode = nil
if e[i].len == 3 and optFieldCheck in p.options:
check = e[i][2]
Expand Down
2 changes: 1 addition & 1 deletion compiler/sem.nim
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ proc fixupTypeAfterEval(c: PContext, evaluated, eOrig: PNode): PNode =
if hasCycle(result):
result = localErrorNode(c, eOrig, "the resulting AST is cyclic and cannot be processed further")
else:
result = semmacrosanity.annotateType(result, expectedType, c.config)
semmacrosanity.annotateType(result, expectedType, c.config)
else:
result = semExprWithType(c, evaluated)
#result = fitNode(c, e.typ, result) inlined with special case:
Expand Down
24 changes: 10 additions & 14 deletions compiler/semmacrosanity.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ proc ithField(t: PType, field: var FieldTracker): FieldInfo =
base = b.baseClass
result = ithField(t.n, field)

proc annotateType*(n: PNode, t: PType; conf: ConfigRef): PNode =
result = n
proc annotateType*(n: PNode, t: PType; conf: ConfigRef) =
let x = t.skipTypes(abstractInst+{tyRange})
# Note: x can be unequal to t and we need to be careful to use 't'
# to not to skip tyGenericInst
Expand All @@ -96,24 +95,23 @@ proc annotateType*(n: PNode, t: PType; conf: ConfigRef): PNode =
let x = t.skipTypes(abstractPtrs)
n.typ() = t
n[0].typ() = t
result = copyNode(n)
result.add(n[0])
for i in 1..<n.len:
var tracker = FieldTracker(index: i-1, remaining: i-1, constr: n, delete: false)
let field = x.ithField(tracker)
if field.isNil:
globalError conf, n.info, "invalid field at index " & $i
elif not field.delete:
else:
# only add fields from active case branches
internalAssert(conf, n[i].kind == nkExprColonExpr)
n[i][1] = annotateType(n[i][1], field.sym.typ, conf)
result.add(n[i])
annotateType(n[i][1], field.sym.typ, conf)
if field.delete:
incl(n[i].flags, nfPreventCg)
of nkPar, nkTupleConstr:
if x.kind == tyTuple:
n.typ() = t
for i in 0..<n.len:
if i >= x.kidsLen: globalError conf, n.info, "invalid field at index " & $i
else: n[i] = annotateType(n[i], x[i], conf)
else: annotateType(n[i], x[i], conf)
elif x.kind == tyProc and x.callConv == ccClosure:
n.typ() = t
elif x.kind == tyOpenArray: # `opcSlice` transforms slices into tuples
Expand All @@ -127,11 +125,11 @@ proc annotateType*(n: PNode, t: PType; conf: ConfigRef): PNode =
of nkStrKinds:
for i in left..right:
bracketExpr.add newIntNode(nkCharLit, BiggestInt n[0].strVal[i])
bracketExpr[^1] = annotateType(bracketExpr[^1], x.elementType, conf)
annotateType(bracketExpr[^1], x.elementType, conf)
of nkBracket:
for i in left..right:
bracketExpr.add n[0][i]
bracketExpr[^1] = annotateType(bracketExpr[^1], x.elementType, conf)
annotateType(bracketExpr[^1], x.elementType, conf)
else:
globalError(conf, n.info, "Incorrectly generated tuple constr")
n[] = bracketExpr[]
Expand All @@ -142,15 +140,13 @@ proc annotateType*(n: PNode, t: PType; conf: ConfigRef): PNode =
of nkBracket:
if x.kind in {tyArray, tySequence, tyOpenArray}:
n.typ() = t
for i in 0 ..< n.len:
n[i] = annotateType(n[i], x.elemType, conf)
for m in n: annotateType(m, x.elemType, conf)
else:
globalError(conf, n.info, "[] must have some form of array type")
of nkCurly:
if x.kind in {tySet}:
n.typ() = t
for i in 0 ..< n.len:
n[i] = annotateType(n[i], x.elemType, conf)
for m in n: annotateType(m, x.elemType, conf)
else:
globalError(conf, n.info, "{} must have the set type")
of nkFloatLit..nkFloat128Lit:
Expand Down
4 changes: 2 additions & 2 deletions compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1458,10 +1458,10 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
var macroCall = newNodeI(nkCall, c.debug[pc])
macroCall.add(newSymNode(prc))
for i in 1..rc-1:
var node = regs[rb+i].regToNode
let node = regs[rb+i].regToNode
node.info = c.debug[pc]
if prc.typ[i].kind notin {tyTyped, tyUntyped}:
node = node.annotateType(prc.typ[i], c.config)
node.annotateType(prc.typ[i], c.config)

macroCall.add(node)
var a = evalTemplate(macroCall, prc, genSymOwner, c.config, c.cache, c.templInstCounter, c.idgen)
Expand Down

0 comments on commit 304f51e

Please sign in to comment.