semfields.nim 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. # either 'tup[i]' or 'field' is valid
  63. var fc = TFieldInstCtx(
  64. c: c.c,
  65. field: typ.sym,
  66. replaceByFieldName: c.m == mFieldPairs
  67. )
  68. openScope(c.c)
  69. inc c.c.inUnrolledContext
  70. let body = instFieldLoopBody(fc, lastSon(forLoop), forLoop)
  71. father.add(semStmt(c.c, body, {}))
  72. dec c.c.inUnrolledContext
  73. closeScope(c.c)
  74. of nkNilLit: discard
  75. of nkRecCase:
  76. let call = forLoop[^2]
  77. if call.len > 2:
  78. localError(c.c.config, forLoop.info,
  79. "parallel 'fields' iterator does not work for 'case' objects")
  80. return
  81. # iterate over the selector:
  82. semForObjectFields(c, typ[0], forLoop, father)
  83. # we need to generate a case statement:
  84. var caseStmt = newNodeI(nkCaseStmt, forLoop.info)
  85. # generate selector:
  86. var access = newNodeI(nkDotExpr, forLoop.info, 2)
  87. access[0] = call[1]
  88. access[1] = newSymNode(typ[0].sym, forLoop.info)
  89. caseStmt.add(semExprWithType(c.c, access))
  90. # copy the branches over, but replace the fields with the for loop body:
  91. for i in 1..<typ.len:
  92. var branch = copyTree(typ[i])
  93. branch[^1] = newNodeI(nkStmtList, forLoop.info)
  94. semForObjectFields(c, typ[i].lastSon, forLoop, branch[^1])
  95. caseStmt.add(branch)
  96. father.add(caseStmt)
  97. of nkRecList:
  98. for t in items(typ): semForObjectFields(c, t, forLoop, father)
  99. else:
  100. illFormedAstLocal(typ, c.c.config)
  101. proc semForFields(c: PContext, n: PNode, m: TMagic): PNode =
  102. # so that 'break' etc. work as expected, we produce
  103. # a 'while true: stmt; break' loop ...
  104. result = newNodeI(nkWhileStmt, n.info, 2)
  105. var trueSymbol = systemModuleSym(c.graph, getIdent(c.cache, "true"))
  106. if trueSymbol == nil:
  107. localError(c.config, n.info, "system needs: 'true'")
  108. trueSymbol = newSym(skUnknown, getIdent(c.cache, "true"), c.idgen, getCurrOwner(c), n.info)
  109. trueSymbol.typ = getSysType(c.graph, n.info, tyBool)
  110. result[0] = newSymNode(trueSymbol, n.info)
  111. var stmts = newNodeI(nkStmtList, n.info)
  112. result[1] = stmts
  113. var call = n[^2]
  114. if n.len-2 != call.len-1 + ord(m==mFieldPairs):
  115. localError(c.config, n.info, errWrongNumberOfVariables)
  116. return result
  117. const skippedTypesForFields = abstractVar - {tyTypeDesc} + tyUserTypeClasses
  118. var tupleTypeA = skipTypes(call[1].typ, skippedTypesForFields)
  119. if tupleTypeA.kind notin {tyTuple, tyObject}:
  120. localError(c.config, n.info, errGenerated, "no object or tuple type")
  121. return result
  122. for i in 1..<call.len:
  123. let calli = call[i]
  124. var tupleTypeB = skipTypes(calli.typ, skippedTypesForFields)
  125. if not sameType(tupleTypeA, tupleTypeB):
  126. typeMismatch(c.config, calli.info, tupleTypeA, tupleTypeB, calli)
  127. inc(c.p.nestedLoopCounter)
  128. let oldBreakInLoop = c.p.breakInLoop
  129. c.p.breakInLoop = true
  130. if tupleTypeA.kind == tyTuple:
  131. var loopBody = n[^1]
  132. for i in 0..<tupleTypeA.len:
  133. openScope(c)
  134. var fc = TFieldInstCtx(
  135. tupleType: tupleTypeA,
  136. tupleIndex: i,
  137. c: c,
  138. replaceByFieldName: m == mFieldPairs
  139. )
  140. var body = instFieldLoopBody(fc, loopBody, n)
  141. inc c.inUnrolledContext
  142. stmts.add(semStmt(c, body, {}))
  143. dec c.inUnrolledContext
  144. closeScope(c)
  145. else:
  146. var fc = TFieldsCtx(m: m, c: c)
  147. var t = tupleTypeA
  148. while t.kind == tyObject:
  149. semForObjectFields(fc, t.n, n, stmts)
  150. if t.baseClass == nil: break
  151. t = skipTypes(t.baseClass, skipPtrs)
  152. c.p.breakInLoop = oldBreakInLoop
  153. dec(c.p.nestedLoopCounter)
  154. # for TR macros this 'while true: ...; break' loop is pretty bad, so
  155. # we avoid it now if we can:
  156. if containsNode(stmts, {nkBreakStmt}):
  157. var b = newNodeI(nkBreakStmt, n.info)
  158. b.add(newNodeI(nkEmpty, n.info))
  159. stmts.add(b)
  160. else:
  161. result = stmts