semfields.nim 5.9 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 = n
  24. of nkIdent, nkSym:
  25. result = n
  26. let ident = considerQuotedIdent(c.c, n)
  27. var L = sonsLen(forLoop)
  28. if c.replaceByFieldName:
  29. if ident.id == considerQuotedIdent(c.c, forLoop[0]).id:
  30. let fieldName = if c.tupleType.isNil: c.field.name.s
  31. elif c.tupleType.n.isNil: "Field" & $c.tupleIndex
  32. else: c.tupleType.n.sons[c.tupleIndex].sym.name.s
  33. result = newStrNode(nkStrLit, fieldName)
  34. return
  35. # other fields:
  36. for i in ord(c.replaceByFieldName)..L-3:
  37. if ident.id == considerQuotedIdent(c.c, forLoop[i]).id:
  38. var call = forLoop.sons[L-2]
  39. var tupl = call.sons[i+1-ord(c.replaceByFieldName)]
  40. if c.field.isNil:
  41. result = newNodeI(nkBracketExpr, n.info)
  42. result.add(tupl)
  43. result.add(newIntNode(nkIntLit, c.tupleIndex))
  44. else:
  45. result = newNodeI(nkDotExpr, n.info)
  46. result.add(tupl)
  47. result.add(newSymNode(c.field, n.info))
  48. break
  49. else:
  50. if n.kind == nkContinueStmt:
  51. localError(c.c.config, n.info,
  52. "'continue' not supported in a 'fields' loop")
  53. result = copyNode(n)
  54. newSons(result, sonsLen(n))
  55. for i in 0 ..< sonsLen(n):
  56. result.sons[i] = instFieldLoopBody(c, n.sons[i], forLoop)
  57. type
  58. TFieldsCtx = object
  59. c: PContext
  60. m: TMagic
  61. proc semForObjectFields(c: TFieldsCtx, typ, forLoop, father: PNode) =
  62. case typ.kind
  63. of nkSym:
  64. var fc: TFieldInstCtx # either 'tup[i]' or 'field' is valid
  65. fc.c = c.c
  66. fc.field = typ.sym
  67. fc.replaceByFieldName = c.m == mFieldPairs
  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 L = forLoop.len
  77. let call = forLoop.sons[L-2]
  78. if call.len > 2:
  79. localError(c.c.config, forLoop.info,
  80. "parallel 'fields' iterator does not work for 'case' objects")
  81. return
  82. # iterate over the selector:
  83. semForObjectFields(c, typ[0], forLoop, father)
  84. # we need to generate a case statement:
  85. var caseStmt = newNodeI(nkCaseStmt, forLoop.info)
  86. # generate selector:
  87. var access = newNodeI(nkDotExpr, forLoop.info, 2)
  88. access.sons[0] = call.sons[1]
  89. access.sons[1] = newSymNode(typ.sons[0].sym, forLoop.info)
  90. caseStmt.add(semExprWithType(c.c, access))
  91. # copy the branches over, but replace the fields with the for loop body:
  92. for i in 1 ..< typ.len:
  93. var branch = copyTree(typ[i])
  94. let L = branch.len
  95. branch.sons[L-1] = newNodeI(nkStmtList, forLoop.info)
  96. semForObjectFields(c, typ[i].lastSon, forLoop, branch[L-1])
  97. caseStmt.add(branch)
  98. father.add(caseStmt)
  99. of nkRecList:
  100. for t in items(typ): semForObjectFields(c, t, forLoop, father)
  101. else:
  102. illFormedAstLocal(typ, c.c.config)
  103. proc semForFields(c: PContext, n: PNode, m: TMagic): PNode =
  104. # so that 'break' etc. work as expected, we produce
  105. # a 'while true: stmt; break' loop ...
  106. result = newNodeI(nkWhileStmt, n.info, 2)
  107. var trueSymbol = strTableGet(c.graph.systemModule.tab, getIdent(c.cache, "true"))
  108. if trueSymbol == nil:
  109. localError(c.config, n.info, "system needs: 'true'")
  110. trueSymbol = newSym(skUnknown, getIdent(c.cache, "true"), getCurrOwner(c), n.info)
  111. trueSymbol.typ = getSysType(c.graph, n.info, tyBool)
  112. result.sons[0] = newSymNode(trueSymbol, n.info)
  113. var stmts = newNodeI(nkStmtList, n.info)
  114. result.sons[1] = stmts
  115. var length = sonsLen(n)
  116. var call = n.sons[length-2]
  117. if length-2 != sonsLen(call)-1 + ord(m==mFieldPairs):
  118. localError(c.config, n.info, errWrongNumberOfVariables)
  119. return result
  120. const skippedTypesForFields = abstractVar - {tyTypeDesc} + tyUserTypeClasses
  121. var tupleTypeA = skipTypes(call.sons[1].typ, skippedTypesForFields)
  122. if tupleTypeA.kind notin {tyTuple, tyObject}:
  123. localError(c.config, n.info, errGenerated, "no object or tuple type")
  124. return result
  125. for i in 1..call.len-1:
  126. var tupleTypeB = skipTypes(call.sons[i].typ, skippedTypesForFields)
  127. if not sameType(tupleTypeA, tupleTypeB):
  128. typeMismatch(c.config, call.sons[i].info, tupleTypeA, tupleTypeB)
  129. inc(c.p.nestedLoopCounter)
  130. if tupleTypeA.kind == tyTuple:
  131. var loopBody = n.sons[length-1]
  132. for i in 0..sonsLen(tupleTypeA)-1:
  133. openScope(c)
  134. var fc: TFieldInstCtx
  135. fc.tupleType = tupleTypeA
  136. fc.tupleIndex = i
  137. fc.c = c
  138. fc.replaceByFieldName = m == mFieldPairs
  139. var body = instFieldLoopBody(fc, loopBody, n)
  140. inc c.inUnrolledContext
  141. stmts.add(semStmt(c, body, {}))
  142. dec c.inUnrolledContext
  143. closeScope(c)
  144. else:
  145. var fc: TFieldsCtx
  146. fc.m = m
  147. fc.c = c
  148. var t = tupleTypeA
  149. while t.kind == tyObject:
  150. semForObjectFields(fc, t.n, n, stmts)
  151. if t.sons[0] == nil: break
  152. t = skipTypes(t.sons[0], skipPtrs)
  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