# # # The Nim Compiler # (c) Copyright 2015 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # ## This module does the semantic transformation of the fields* iterators. # included from semstmts.nim type TFieldInstCtx = object # either 'tup[i]' or 'field' is valid tupleType: PType # if != nil we're traversing a tuple tupleIndex: int field: PSym replaceByFieldName: bool c: PContext proc instFieldLoopBody(c: TFieldInstCtx, n: PNode, forLoop: PNode): PNode = if c.field != nil and isEmptyType(c.field.typ): result = newNode(nkEmpty) return case n.kind of nkEmpty..pred(nkIdent), succ(nkSym)..nkNilLit: result = copyNode(n) of nkIdent, nkSym: result = n let ident = considerQuotedIdent(c.c, n) if c.replaceByFieldName: if ident.id == considerQuotedIdent(c.c, forLoop[0]).id: let fieldName = if c.tupleType.isNil: c.field.name.s elif c.tupleType.n.isNil: "Field" & $c.tupleIndex else: c.tupleType.n[c.tupleIndex].sym.name.s result = newStrNode(nkStrLit, fieldName) return # other fields: for i in ord(c.replaceByFieldName).. 2: localError(c.c.config, forLoop.info, "parallel 'fields' iterator does not work for 'case' objects") return # iterate over the selector: semForObjectFields(c, typ[0], forLoop, father) # we need to generate a case statement: var caseStmt = newNodeI(nkCaseStmt, forLoop.info) # generate selector: var access = newNodeI(nkDotExpr, forLoop.info, 2) access[0] = call[1] access[1] = newSymNode(typ[0].sym, forLoop.info) caseStmt.add(semExprWithType(c.c, access)) # copy the branches over, but replace the fields with the for loop body: for i in 1..