semfields.nim 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2015 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module does the semantic transformation of the fields* iterators.
  10. # included from semstmts.nim
  11. type
  12. TFieldInstCtx = object # either 'tup[i]' or 'field' is valid
  13. tupleType: PType # if != nil we're traversing a tuple
  14. tupleIndex: int
  15. field: PSym
  16. replaceByFieldName: bool
  17. c: PContext
  18. proc instFieldLoopBody(c: TFieldInstCtx, n: PNode, forLoop: PNode): PNode =
  19. if c.field != nil and isEmptyType(c.field.typ):
  20. result = newNode(nkEmpty)
  21. return
  22. case n.kind
  23. of nkEmpty..pred(nkIdent), succ(nkSym)..nkNilLit: result = copyNode(n)
  24. of nkIdent, nkSym:
  25. result = n
  26. let ident = considerQuotedIdent(c.c, n)
  27. if c.replaceByFieldName:
  28. if ident.id == considerQuotedIdent(c.c, forLoop[0]).id:
  29. let fieldName = if c.tupleType.isNil: c.field.name.s
  30. elif c.tupleType.n.isNil: "Field" & $c.tupleIndex
  31. else: c.tupleType.n[c.tupleIndex].sym.name.s
  32. result = newStrNode(nkStrLit, fieldName)
  33. return
  34. # other fields:
  35. for i in ord(c.replaceByFieldName)..<forLoop.len-2:
  36. if ident.id == considerQuotedIdent(c.c, forLoop[i]).id:
  37. var call = forLoop[^2]
  38. var tupl = call[i+1-ord(c.replaceByFieldName)]
  39. if c.field.isNil:
  40. result = newNodeI(nkBracketExpr, n.info)
  41. result.add(tupl)
  42. result.add(newIntNode(nkIntLit, c.tupleIndex))
  43. else:
  44. result = newNodeI(nkDotExpr, n.info)
  45. result.add(tupl)
  46. result.add(newSymNode(c.field, n.info))
  47. break
  48. else:
  49. if n.kind == nkContinueStmt:
  50. localError(c.c.config, n.info,
  51. "'continue' not supported in a 'fields' loop")
  52. result = shallowCopy(n)
  53. for i in 0..<n.len:
  54. result[i] = instFieldLoopBody(c, n[i], forLoop)
  55. type
  56. TFieldsCtx = object
  57. c: PContext
  58. m: TMagic
  59. proc semForObjectFields(c: TFieldsCtx, typ, forLoop, father: PNode) =
  60. case typ.kind
  61. of nkSym:
  62. var fc: TFieldInstCtx # either 'tup[i]' or 'field' is valid
  63. fc.c = c.c
  64. fc.field = typ.sym
  65. fc.replaceByFieldName = c.m == mFieldPairs
  66. openScope(c.c)
  67. inc c.c.inUnrolledContext
  68. let body = instFieldLoopBody(fc, lastSon(forLoop), forLoop)
  69. father.add(semStmt(c.c, body, {}))
  70. dec c.c.inUnrolledContext
  71. closeScope(c.c)
  72. of nkNilLit: discard
  73. of nkRecCase:
  74. let call = forLoop[^2]
  75. if call.len > 2:
  76. localError(c.c.config, forLoop.info,
  77. "parallel 'fields' iterator does not work for 'case' objects")
  78. return
  79. # iterate over the selector:
  80. semForObjectFields(c, typ[0], forLoop, father)
  81. # we need to generate a case statement:
  82. var caseStmt = newNodeI(nkCaseStmt, forLoop.info)
  83. # generate selector:
  84. var access = newNodeI(nkDotExpr, forLoop.info, 2)
  85. access[0] = call[1]
  86. access[1] = newSymNode(typ[0].sym, forLoop.info)
  87. caseStmt.add(semExprWithType(c.c, access))
  88. # copy the branches over, but replace the fields with the for loop body:
  89. for i in 1..<typ.len:
  90. var branch = copyTree(typ[i])
  91. branch[^1] = newNodeI(nkStmtList, forLoop.info)
  92. semForObjectFields(c, typ[i].lastSon, forLoop, branch[^1])
  93. caseStmt.add(branch)
  94. father.add(caseStmt)
  95. of nkRecList:
  96. for t in items(typ): semForObjectFields(c, t, forLoop, father)
  97. else:
  98. illFormedAstLocal(typ, c.c.config)
  99. proc semForFields(c: PContext, n: PNode, m: TMagic): PNode =
  100. # so that 'break' etc. work as expected, we produce
  101. # a 'while true: stmt; break' loop ...
  102. result = newNodeI(nkWhileStmt, n.info, 2)
  103. var trueSymbol = strTableGet(c.graph.systemModule.tab, getIdent(c.cache, "true"))
  104. if trueSymbol == nil:
  105. localError(c.config, n.info, "system needs: 'true'")
  106. trueSymbol = newSym(skUnknown, getIdent(c.cache, "true"), getCurrOwner(c), n.info)
  107. trueSymbol.typ = getSysType(c.graph, n.info, tyBool)
  108. result[0] = newSymNode(trueSymbol, n.info)
  109. var stmts = newNodeI(nkStmtList, n.info)
  110. result[1] = stmts
  111. var call = n[^2]
  112. if n.len-2 != call.len-1 + ord(m==mFieldPairs):
  113. localError(c.config, n.info, errWrongNumberOfVariables)
  114. return result
  115. const skippedTypesForFields = abstractVar - {tyTypeDesc} + tyUserTypeClasses
  116. var tupleTypeA = skipTypes(call[1].typ, skippedTypesForFields)
  117. if tupleTypeA.kind notin {tyTuple, tyObject}:
  118. localError(c.config, n.info, errGenerated, "no object or tuple type")
  119. return result
  120. for i in 1..<call.len:
  121. var tupleTypeB = skipTypes(call[i].typ, skippedTypesForFields)
  122. if not sameType(tupleTypeA, tupleTypeB):
  123. typeMismatch(c.config, call[i].info, tupleTypeA, tupleTypeB)
  124. inc(c.p.nestedLoopCounter)
  125. if tupleTypeA.kind == tyTuple:
  126. var loopBody = n[^1]
  127. for i in 0..<tupleTypeA.len:
  128. openScope(c)
  129. var fc: TFieldInstCtx
  130. fc.tupleType = tupleTypeA
  131. fc.tupleIndex = i
  132. fc.c = c
  133. fc.replaceByFieldName = m == mFieldPairs
  134. var body = instFieldLoopBody(fc, loopBody, n)
  135. inc c.inUnrolledContext
  136. stmts.add(semStmt(c, body, {}))
  137. dec c.inUnrolledContext
  138. closeScope(c)
  139. else:
  140. var fc: TFieldsCtx
  141. fc.m = m
  142. fc.c = c
  143. var t = tupleTypeA
  144. while t.kind == tyObject:
  145. semForObjectFields(fc, t.n, n, stmts)
  146. if t[0] == nil: break
  147. t = skipTypes(t[0], skipPtrs)
  148. dec(c.p.nestedLoopCounter)
  149. # for TR macros this 'while true: ...; break' loop is pretty bad, so
  150. # we avoid it now if we can:
  151. if containsNode(stmts, {nkBreakStmt}):
  152. var b = newNodeI(nkBreakStmt, n.info)
  153. b.add(newNodeI(nkEmpty, n.info))
  154. stmts.add(b)
  155. else:
  156. result = stmts