semfields.nim 5.8 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. var L = len(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 = shallowCopy(n)
  54. for i in 0 ..< len(n):
  55. result.sons[i] = instFieldLoopBody(c, n.sons[i], forLoop)
  56. type
  57. TFieldsCtx = object
  58. c: PContext
  59. m: TMagic
  60. proc semForObjectFields(c: TFieldsCtx, typ, forLoop, father: PNode) =
  61. case typ.kind
  62. of nkSym:
  63. var fc: TFieldInstCtx # either 'tup[i]' or 'field' is valid
  64. fc.c = c.c
  65. fc.field = typ.sym
  66. fc.replaceByFieldName = c.m == mFieldPairs
  67. openScope(c.c)
  68. inc c.c.inUnrolledContext
  69. let body = instFieldLoopBody(fc, lastSon(forLoop), forLoop)
  70. father.add(semStmt(c.c, body, {}))
  71. dec c.c.inUnrolledContext
  72. closeScope(c.c)
  73. of nkNilLit: discard
  74. of nkRecCase:
  75. let L = forLoop.len
  76. let call = forLoop.sons[L-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.sons[0] = call.sons[1]
  88. access.sons[1] = newSymNode(typ.sons[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. let L = branch.len
  94. branch.sons[L-1] = newNodeI(nkStmtList, forLoop.info)
  95. semForObjectFields(c, typ[i].lastSon, forLoop, branch[L-1])
  96. caseStmt.add(branch)
  97. father.add(caseStmt)
  98. of nkRecList:
  99. for t in items(typ): semForObjectFields(c, t, forLoop, father)
  100. else:
  101. illFormedAstLocal(typ, c.c.config)
  102. proc semForFields(c: PContext, n: PNode, m: TMagic): PNode =
  103. # so that 'break' etc. work as expected, we produce
  104. # a 'while true: stmt; break' loop ...
  105. result = newNodeI(nkWhileStmt, n.info, 2)
  106. var trueSymbol = strTableGet(c.graph.systemModule.tab, getIdent(c.cache, "true"))
  107. if trueSymbol == nil:
  108. localError(c.config, n.info, "system needs: 'true'")
  109. trueSymbol = newSym(skUnknown, getIdent(c.cache, "true"), getCurrOwner(c), n.info)
  110. trueSymbol.typ = getSysType(c.graph, n.info, tyBool)
  111. result.sons[0] = newSymNode(trueSymbol, n.info)
  112. var stmts = newNodeI(nkStmtList, n.info)
  113. result.sons[1] = stmts
  114. var length = len(n)
  115. var call = n.sons[length-2]
  116. if length-2 != len(call)-1 + ord(m==mFieldPairs):
  117. localError(c.config, n.info, errWrongNumberOfVariables)
  118. return result
  119. const skippedTypesForFields = abstractVar - {tyTypeDesc} + tyUserTypeClasses
  120. var tupleTypeA = skipTypes(call.sons[1].typ, skippedTypesForFields)
  121. if tupleTypeA.kind notin {tyTuple, tyObject}:
  122. localError(c.config, n.info, errGenerated, "no object or tuple type")
  123. return result
  124. for i in 1..call.len-1:
  125. var tupleTypeB = skipTypes(call.sons[i].typ, skippedTypesForFields)
  126. if not sameType(tupleTypeA, tupleTypeB):
  127. typeMismatch(c.config, call.sons[i].info, tupleTypeA, tupleTypeB)
  128. inc(c.p.nestedLoopCounter)
  129. if tupleTypeA.kind == tyTuple:
  130. var loopBody = n.sons[length-1]
  131. for i in 0..len(tupleTypeA)-1:
  132. openScope(c)
  133. var fc: TFieldInstCtx
  134. fc.tupleType = tupleTypeA
  135. fc.tupleIndex = i
  136. fc.c = c
  137. fc.replaceByFieldName = m == mFieldPairs
  138. var body = instFieldLoopBody(fc, loopBody, n)
  139. inc c.inUnrolledContext
  140. stmts.add(semStmt(c, body, {}))
  141. dec c.inUnrolledContext
  142. closeScope(c)
  143. else:
  144. var fc: TFieldsCtx
  145. fc.m = m
  146. fc.c = c
  147. var t = tupleTypeA
  148. while t.kind == tyObject:
  149. semForObjectFields(fc, t.n, n, stmts)
  150. if t.sons[0] == nil: break
  151. t = skipTypes(t.sons[0], skipPtrs)
  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