semdestruct.nim 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements destructors.
  10. # included from sem.nim
  11. # special marker values that indicates that we are
  12. # 1) AnalyzingDestructor: currently analyzing the type for destructor
  13. # generation (needed for recursive types)
  14. # 2) DestructorIsTrivial: completed the analysis before and determined
  15. # that the type has a trivial destructor
  16. var analyzingDestructor, destructorIsTrivial: PSym
  17. new(analyzingDestructor)
  18. new(destructorIsTrivial)
  19. var
  20. destructorName = getIdent"destroy_"
  21. destructorParam = getIdent"this_"
  22. destructorPragma = newIdentNode(getIdent"destructor", unknownLineInfo())
  23. proc instantiateDestructor(c: PContext, typ: PType): PType
  24. proc doDestructorStuff(c: PContext, s: PSym, n: PNode) =
  25. var t = s.typ.sons[1].skipTypes({tyVar})
  26. if t.kind == tyGenericInvocation:
  27. for i in 1 .. <t.sonsLen:
  28. if t.sons[i].kind != tyGenericParam:
  29. localError(n.info, errDestructorNotGenericEnough)
  30. return
  31. t = t.base
  32. elif t.kind == tyCompositeTypeClass:
  33. t = t.base
  34. if t.kind != tyGenericBody:
  35. localError(n.info, errDestructorNotGenericEnough)
  36. return
  37. t.destructor = s
  38. # automatically insert calls to base classes' destructors
  39. if n.sons[bodyPos].kind != nkEmpty:
  40. for i in countup(0, t.sonsLen - 1):
  41. # when inheriting directly from object
  42. # there will be a single nil son
  43. if t.sons[i] == nil: continue
  44. let destructableT = instantiateDestructor(c, t.sons[i])
  45. if destructableT != nil:
  46. n.sons[bodyPos].addSon(newNode(nkCall, t.sym.info, @[
  47. useSym(destructableT.destructor, c.graph.usageSym),
  48. n.sons[paramsPos][1][0]]))
  49. proc destroyFieldOrFields(c: PContext, field: PNode, holder: PNode): PNode
  50. proc destroySym(c: PContext, field: PSym, holder: PNode): PNode =
  51. let destructableT = instantiateDestructor(c, field.typ)
  52. if destructableT != nil:
  53. result = newNode(nkCall, field.info, @[
  54. useSym(destructableT.destructor, c.graph.usageSym),
  55. newNode(nkDotExpr, field.info, @[holder, useSym(field, c.graph.usageSym)])])
  56. proc destroyCase(c: PContext, n: PNode, holder: PNode): PNode =
  57. var nonTrivialFields = 0
  58. result = newNode(nkCaseStmt, n.info, @[])
  59. # case x.kind
  60. result.addSon(newNode(nkDotExpr, n.info, @[holder, n.sons[0]]))
  61. for i in countup(1, n.len - 1):
  62. # of A, B:
  63. let ni = n[i]
  64. var caseBranch = newNode(ni.kind, ni.info, ni.sons[0..ni.len-2])
  65. let stmt = destroyFieldOrFields(c, ni.lastSon, holder)
  66. if stmt == nil:
  67. caseBranch.addSon(newNode(nkStmtList, ni.info, @[]))
  68. else:
  69. caseBranch.addSon(stmt)
  70. nonTrivialFields += stmt.len
  71. result.addSon(caseBranch)
  72. # maybe no fields were destroyed?
  73. if nonTrivialFields == 0:
  74. result = nil
  75. proc destroyFieldOrFields(c: PContext, field: PNode, holder: PNode): PNode =
  76. template maybeAddLine(e) =
  77. let stmt = e
  78. if stmt != nil:
  79. if result == nil: result = newNode(nkStmtList)
  80. result.addSon(stmt)
  81. case field.kind
  82. of nkRecCase:
  83. maybeAddLine destroyCase(c, field, holder)
  84. of nkSym:
  85. maybeAddLine destroySym(c, field.sym, holder)
  86. of nkRecList:
  87. for son in field:
  88. maybeAddLine destroyFieldOrFields(c, son, holder)
  89. else:
  90. internalAssert false
  91. proc generateDestructor(c: PContext, t: PType): PNode =
  92. ## generate a destructor for a user-defined object or tuple type
  93. ## returns nil if the destructor turns out to be trivial
  94. # XXX: This may be true for some C-imported types such as
  95. # Tposix_spawnattr
  96. if t.n == nil or t.n.sons == nil: return
  97. internalAssert t.n.kind == nkRecList
  98. let destructedObj = newIdentNode(destructorParam, unknownLineInfo())
  99. # call the destructods of all fields
  100. result = destroyFieldOrFields(c, t.n, destructedObj)
  101. # base classes' destructors will be automatically called by
  102. # semProcAux for both auto-generated and user-defined destructors
  103. proc instantiateDestructor(c: PContext, typ: PType): PType =
  104. # returns nil if a variable of type `typ` doesn't require a
  105. # destructor. Otherwise, returns the type, which holds the
  106. # destructor that must be used for the varialbe.
  107. # The destructor is either user-defined or automatically
  108. # generated by the compiler in a member-wise fashion.
  109. var t = typ.skipGenericAlias
  110. let typeHoldingUserDefinition = if t.kind == tyGenericInst: t.base else: t
  111. if typeHoldingUserDefinition.destructor != nil:
  112. # XXX: This is not entirely correct for recursive types, but we need
  113. # it temporarily to hide the "destroy is already defined" problem
  114. if typeHoldingUserDefinition.destructor notin
  115. [analyzingDestructor, destructorIsTrivial]:
  116. return typeHoldingUserDefinition
  117. else:
  118. return nil
  119. t = t.skipTypes({tyGenericInst, tyAlias})
  120. case t.kind
  121. of tySequence, tyArray, tyOpenArray, tyVarargs:
  122. t.destructor = analyzingDestructor
  123. if instantiateDestructor(c, t.sons[0]) != nil:
  124. t.destructor = getCompilerProc"nimDestroyRange"
  125. return t
  126. else:
  127. return nil
  128. of tyTuple, tyObject:
  129. t.destructor = analyzingDestructor
  130. let generated = generateDestructor(c, t)
  131. if generated != nil:
  132. internalAssert t.sym != nil
  133. var i = t.sym.info
  134. let fullDef = newNode(nkProcDef, i, @[
  135. newIdentNode(destructorName, i),
  136. emptyNode,
  137. emptyNode,
  138. newNode(nkFormalParams, i, @[
  139. emptyNode,
  140. newNode(nkIdentDefs, i, @[
  141. newIdentNode(destructorParam, i),
  142. symNodeFromType(c, makeVarType(c, t), t.sym.info),
  143. emptyNode]),
  144. ]),
  145. newNode(nkPragma, i, @[destructorPragma]),
  146. emptyNode,
  147. generated
  148. ])
  149. let semantizedDef = semProc(c, fullDef)
  150. t.destructor = semantizedDef[namePos].sym
  151. return t
  152. else:
  153. t.destructor = destructorIsTrivial
  154. return nil
  155. else:
  156. return nil
  157. proc createDestructorCall(c: PContext, s: PSym): PNode =
  158. let varTyp = s.typ
  159. if varTyp == nil or sfGlobal in s.flags: return
  160. let destructableT = instantiateDestructor(c, varTyp)
  161. if destructableT != nil:
  162. let call = semStmt(c, newNode(nkCall, s.info, @[
  163. useSym(destructableT.destructor, c.graph.usageSym),
  164. useSym(s, c.graph.usageSym)]))
  165. result = newNode(nkDefer, s.info, @[call])