semfields.nim 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 wrapNewScope(c: PContext, n: PNode): PNode {.inline.} =
  19. # use `if true` to not interfere with `break`
  20. # just opening scope via `openScope(c)` isn't enough,
  21. # a scope has to be opened in the codegen as well for reused
  22. # template instantiations
  23. let trueLit = newIntLit(c.graph, n.info, 1)
  24. trueLit.typ() = getSysType(c.graph, n.info, tyBool)
  25. result = newTreeI(nkIfStmt, n.info, newTreeI(nkElifBranch, n.info, trueLit, n))
  26. proc instFieldLoopBody(c: TFieldInstCtx, n: PNode, forLoop: PNode): PNode =
  27. if c.field != nil and isEmptyType(c.field.typ):
  28. result = newNode(nkEmpty)
  29. return
  30. case n.kind
  31. of nkEmpty..pred(nkIdent), succ(nkSym)..nkNilLit: result = copyNode(n)
  32. of nkIdent, nkSym:
  33. result = n
  34. let ident = considerQuotedIdent(c.c, n)
  35. if c.replaceByFieldName:
  36. if ident.id == considerQuotedIdent(c.c, forLoop[0]).id:
  37. let fieldName = if c.tupleType.isNil: c.field.name.s
  38. elif c.tupleType.n.isNil: "Field" & $c.tupleIndex
  39. else: c.tupleType.n[c.tupleIndex].sym.name.s
  40. result = newStrNode(nkStrLit, fieldName)
  41. return
  42. # other fields:
  43. for i in ord(c.replaceByFieldName)..<forLoop.len-2:
  44. if ident.id == considerQuotedIdent(c.c, forLoop[i]).id:
  45. var call = forLoop[^2]
  46. var tupl = call[i+1-ord(c.replaceByFieldName)]
  47. if c.field.isNil:
  48. result = newNodeI(nkBracketExpr, n.info)
  49. result.add(tupl)
  50. result.add(newIntNode(nkIntLit, c.tupleIndex))
  51. else:
  52. result = newNodeI(nkDotExpr, n.info)
  53. result.add(tupl)
  54. result.add(newSymNode(c.field, n.info))
  55. break
  56. else:
  57. if n.kind == nkContinueStmt:
  58. localError(c.c.config, n.info,
  59. "'continue' not supported in a 'fields' loop")
  60. result = shallowCopy(n)
  61. for i in 0..<n.len:
  62. result[i] = instFieldLoopBody(c, n[i], forLoop)
  63. type
  64. TFieldsCtx = object
  65. c: PContext
  66. m: TMagic
  67. proc semForObjectFields(c: TFieldsCtx, typ, forLoop, father: PNode) =
  68. case typ.kind
  69. of nkSym:
  70. # either 'tup[i]' or 'field' is valid
  71. var fc = TFieldInstCtx(
  72. c: c.c,
  73. field: typ.sym,
  74. replaceByFieldName: c.m == mFieldPairs
  75. )
  76. openScope(c.c)
  77. inc c.c.inUnrolledContext
  78. var body = instFieldLoopBody(fc, lastSon(forLoop), forLoop)
  79. # new scope for each field that codegen should know about:
  80. body = wrapNewScope(c.c, body)
  81. father.add(semStmt(c.c, body, {}))
  82. dec c.c.inUnrolledContext
  83. closeScope(c.c)
  84. of nkNilLit: discard
  85. of nkRecCase:
  86. let call = forLoop[^2]
  87. if call.len > 2:
  88. localError(c.c.config, forLoop.info,
  89. "parallel 'fields' iterator does not work for 'case' objects")
  90. return
  91. # iterate over the selector:
  92. semForObjectFields(c, typ[0], forLoop, father)
  93. # we need to generate a case statement:
  94. var caseStmt = newNodeI(nkCaseStmt, forLoop.info)
  95. # generate selector:
  96. var access = newNodeI(nkDotExpr, forLoop.info, 2)
  97. access[0] = call[1]
  98. access[1] = newSymNode(typ[0].sym, forLoop.info)
  99. caseStmt.add(semExprWithType(c.c, access))
  100. # copy the branches over, but replace the fields with the for loop body:
  101. for i in 1..<typ.len:
  102. var branch = copyTree(typ[i])
  103. branch[^1] = newNodeI(nkStmtList, forLoop.info)
  104. semForObjectFields(c, typ[i].lastSon, forLoop, branch[^1])
  105. caseStmt.add(branch)
  106. father.add(caseStmt)
  107. of nkRecList:
  108. for t in items(typ): semForObjectFields(c, t, forLoop, father)
  109. else:
  110. illFormedAstLocal(typ, c.c.config)
  111. proc semForFields(c: PContext, n: PNode, m: TMagic): PNode =
  112. # so that 'break' etc. work as expected, we produce
  113. # a 'while true: stmt; break' loop ...
  114. result = newNodeI(nkWhileStmt, n.info, 2)
  115. var trueSymbol = systemModuleSym(c.graph, getIdent(c.cache, "true"))
  116. if trueSymbol == nil:
  117. localError(c.config, n.info, "system needs: 'true'")
  118. trueSymbol = newSym(skUnknown, getIdent(c.cache, "true"), c.idgen, getCurrOwner(c), n.info)
  119. trueSymbol.typ = getSysType(c.graph, n.info, tyBool)
  120. result[0] = newSymNode(trueSymbol, n.info)
  121. var stmts = newNodeI(nkStmtList, n.info)
  122. result[1] = stmts
  123. var call = n[^2]
  124. if n.len-2 != call.len-1 + ord(m==mFieldPairs):
  125. localError(c.config, n.info, errWrongNumberOfVariables)
  126. return result
  127. const skippedTypesForFields = abstractVar - {tyTypeDesc} + tyUserTypeClasses
  128. var tupleTypeA = skipTypes(call[1].typ, skippedTypesForFields)
  129. if tupleTypeA.kind notin {tyTuple, tyObject}:
  130. localError(c.config, n.info, errGenerated, "no object or tuple type")
  131. return result
  132. for i in 1..<call.len:
  133. let calli = call[i]
  134. var tupleTypeB = skipTypes(calli.typ, skippedTypesForFields)
  135. if not sameType(tupleTypeA, tupleTypeB):
  136. typeMismatch(c.config, calli.info, tupleTypeA, tupleTypeB, calli)
  137. inc(c.p.nestedLoopCounter)
  138. let oldBreakInLoop = c.p.breakInLoop
  139. c.p.breakInLoop = true
  140. if tupleTypeA.kind == tyTuple:
  141. var loopBody = n[^1]
  142. for i in 0..<tupleTypeA.len:
  143. openScope(c)
  144. var fc = TFieldInstCtx(
  145. tupleType: tupleTypeA,
  146. tupleIndex: i,
  147. c: c,
  148. replaceByFieldName: m == mFieldPairs
  149. )
  150. var body = instFieldLoopBody(fc, loopBody, n)
  151. # new scope for each field that codegen should know about:
  152. body = wrapNewScope(c, body)
  153. inc c.inUnrolledContext
  154. stmts.add(semStmt(c, body, {}))
  155. dec c.inUnrolledContext
  156. closeScope(c)
  157. else:
  158. var fc = TFieldsCtx(m: m, c: c)
  159. var t = tupleTypeA
  160. while t.kind == tyObject:
  161. semForObjectFields(fc, t.n, n, stmts)
  162. if t.baseClass == nil: break
  163. t = skipTypes(t.baseClass, skipPtrs)
  164. c.p.breakInLoop = oldBreakInLoop
  165. dec(c.p.nestedLoopCounter)
  166. # for TR macros this 'while true: ...; break' loop is pretty bad, so
  167. # we avoid it now if we can:
  168. if containsNode(stmts, {nkBreakStmt}):
  169. var b = newNodeI(nkBreakStmt, n.info)
  170. b.add(newNodeI(nkEmpty, n.info))
  171. stmts.add(b)
  172. else:
  173. result = stmts