semfields.nim 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. case n.kind
  20. of nkEmpty..pred(nkIdent), succ(nkSym)..nkNilLit: result = n
  21. of nkIdent, nkSym:
  22. result = n
  23. let ident = considerQuotedIdent(c.c, n)
  24. var L = sonsLen(forLoop)
  25. if c.replaceByFieldName:
  26. if ident.id == considerQuotedIdent(c.c, forLoop[0]).id:
  27. let fieldName = if c.tupleType.isNil: c.field.name.s
  28. elif c.tupleType.n.isNil: "Field" & $c.tupleIndex
  29. else: c.tupleType.n.sons[c.tupleIndex].sym.name.s
  30. result = newStrNode(nkStrLit, fieldName)
  31. return
  32. # other fields:
  33. for i in ord(c.replaceByFieldName)..L-3:
  34. if ident.id == considerQuotedIdent(c.c, forLoop[i]).id:
  35. var call = forLoop.sons[L-2]
  36. var tupl = call.sons[i+1-ord(c.replaceByFieldName)]
  37. if c.field.isNil:
  38. result = newNodeI(nkBracketExpr, n.info)
  39. result.add(tupl)
  40. result.add(newIntNode(nkIntLit, c.tupleIndex))
  41. else:
  42. result = newNodeI(nkDotExpr, n.info)
  43. result.add(tupl)
  44. result.add(newSymNode(c.field, n.info))
  45. break
  46. else:
  47. if n.kind == nkContinueStmt:
  48. localError(c.c.config, n.info,
  49. "'continue' not supported in a 'fields' loop")
  50. result = copyNode(n)
  51. newSons(result, sonsLen(n))
  52. for i in countup(0, sonsLen(n)-1):
  53. result.sons[i] = instFieldLoopBody(c, n.sons[i], forLoop)
  54. type
  55. TFieldsCtx = object
  56. c: PContext
  57. m: TMagic
  58. proc semForObjectFields(c: TFieldsCtx, typ, forLoop, father: PNode) =
  59. case typ.kind
  60. of nkSym:
  61. var fc: TFieldInstCtx # either 'tup[i]' or 'field' is valid
  62. fc.c = c.c
  63. fc.field = typ.sym
  64. fc.replaceByFieldName = c.m == mFieldPairs
  65. openScope(c.c)
  66. inc c.c.inUnrolledContext
  67. let body = instFieldLoopBody(fc, lastSon(forLoop), forLoop)
  68. father.add(semStmt(c.c, body, {}))
  69. dec c.c.inUnrolledContext
  70. closeScope(c.c)
  71. of nkNilLit: discard
  72. of nkRecCase:
  73. let L = forLoop.len
  74. let call = forLoop.sons[L-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.sons[0] = call.sons[1]
  86. access.sons[1] = newSymNode(typ.sons[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. let L = branch.len
  92. branch.sons[L-1] = newNodeI(nkStmtList, forLoop.info)
  93. semForObjectFields(c, typ[i].lastSon, forLoop, branch[L-1])
  94. caseStmt.add(branch)
  95. father.add(caseStmt)
  96. of nkRecList:
  97. for t in items(typ): semForObjectFields(c, t, forLoop, father)
  98. else:
  99. illFormedAstLocal(typ, c.c.config)
  100. proc semForFields(c: PContext, n: PNode, m: TMagic): PNode =
  101. # so that 'break' etc. work as expected, we produce
  102. # a 'while true: stmt; break' loop ...
  103. result = newNodeI(nkWhileStmt, n.info, 2)
  104. var trueSymbol = strTableGet(c.graph.systemModule.tab, getIdent(c.cache, "true"))
  105. if trueSymbol == nil:
  106. localError(c.config, n.info, "system needs: 'true'")
  107. trueSymbol = newSym(skUnknown, getIdent(c.cache, "true"), getCurrOwner(c), n.info)
  108. trueSymbol.typ = getSysType(c.graph, n.info, tyBool)
  109. result.sons[0] = newSymNode(trueSymbol, n.info)
  110. var stmts = newNodeI(nkStmtList, n.info)
  111. result.sons[1] = stmts
  112. var length = sonsLen(n)
  113. var call = n.sons[length-2]
  114. if length-2 != sonsLen(call)-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.sons[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-1:
  123. var tupleTypeB = skipTypes(call.sons[i].typ, skippedTypesForFields)
  124. if not sameType(tupleTypeA, tupleTypeB):
  125. typeMismatch(c.config, call.sons[i].info, tupleTypeA, tupleTypeB)
  126. inc(c.p.nestedLoopCounter)
  127. if tupleTypeA.kind == tyTuple:
  128. var loopBody = n.sons[length-1]
  129. for i in 0..sonsLen(tupleTypeA)-1:
  130. openScope(c)
  131. var fc: TFieldInstCtx
  132. fc.tupleType = tupleTypeA
  133. fc.tupleIndex = i
  134. fc.c = c
  135. fc.replaceByFieldName = m == mFieldPairs
  136. var body = instFieldLoopBody(fc, loopBody, n)
  137. inc c.inUnrolledContext
  138. stmts.add(semStmt(c, body, {}))
  139. dec c.inUnrolledContext
  140. closeScope(c)
  141. else:
  142. var fc: TFieldsCtx
  143. fc.m = m
  144. fc.c = c
  145. var t = tupleTypeA
  146. while t.kind == tyObject:
  147. semForObjectFields(fc, t.n, n, stmts)
  148. if t.sons[0] == nil: break
  149. t = skipTypes(t.sons[0], skipPtrs)
  150. dec(c.p.nestedLoopCounter)
  151. # for TR macros this 'while true: ...; break' loop is pretty bad, so
  152. # we avoid it now if we can:
  153. if containsNode(stmts, {nkBreakStmt}):
  154. var b = newNodeI(nkBreakStmt, n.info)
  155. b.add(newNodeI(nkEmpty, n.info))
  156. stmts.add(b)
  157. else:
  158. result = stmts