semfields.nim 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 = systemModuleSym(c.graph, 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"), c.idgen, 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. let calli = call[i]
  122. var tupleTypeB = skipTypes(calli.typ, skippedTypesForFields)
  123. if not sameType(tupleTypeA, tupleTypeB):
  124. typeMismatch(c.config, calli.info, tupleTypeA, tupleTypeB, calli)
  125. inc(c.p.nestedLoopCounter)
  126. let oldBreakInLoop = c.p.breakInLoop
  127. c.p.breakInLoop = true
  128. if tupleTypeA.kind == tyTuple:
  129. var loopBody = n[^1]
  130. for i in 0..<tupleTypeA.len:
  131. openScope(c)
  132. var fc: TFieldInstCtx
  133. fc.tupleType = tupleTypeA
  134. fc.tupleIndex = i
  135. fc.c = c
  136. fc.replaceByFieldName = m == mFieldPairs
  137. var body = instFieldLoopBody(fc, loopBody, n)
  138. inc c.inUnrolledContext
  139. stmts.add(semStmt(c, body, {}))
  140. dec c.inUnrolledContext
  141. closeScope(c)
  142. else:
  143. var fc: TFieldsCtx
  144. fc.m = m
  145. fc.c = c
  146. var t = tupleTypeA
  147. while t.kind == tyObject:
  148. semForObjectFields(fc, t.n, n, stmts)
  149. if t[0] == nil: break
  150. t = skipTypes(t[0], skipPtrs)
  151. c.p.breakInLoop = oldBreakInLoop
  152. dec(c.p.nestedLoopCounter)
  153. # for TR macros this 'while true: ...; break' loop is pretty bad, so
  154. # we avoid it now if we can:
  155. if containsNode(stmts, {nkBreakStmt}):
  156. var b = newNodeI(nkBreakStmt, n.info)
  157. b.add(newNodeI(nkEmpty, n.info))
  158. stmts.add(b)
  159. else:
  160. result = stmts