summary refs log tree commit diff stats
path: root/tests/generics
Commit message (Expand)AuthorAgeFilesLines
...
* Merge tests into a larger file (part 3 of ∞: generics) (#9347)Miran2018-10-1465-1276/+1236
* add issue #5707 as test case (#9361)Vindaar2018-10-141-0/+6
* fix tests/generics/t8694.nim: runnableExamples were not run because of #9216 ...Timothee Cour2018-10-091-3/+3
* Fix regression with runnableExamples in generic exprLemonBoy2018-09-302-0/+64
* fixes #8883Araq2018-09-141-0/+657
* fixes #7854Araq2018-08-301-0/+36
* make generic tests green againAndreas Rumpf2018-08-131-2/+2
* Merge branch 'devel' into araq-miscAndreas Rumpf2018-08-102-0/+39
|\
| * Tests for v1 closed generics/static issues (#8572)Mamy Ratsimbazafy2018-08-082-0/+39
* | make test green againAraq2018-08-081-6/+6
|/
* Try conversion to static[T] in generic instantation (#8443)LemonBoy2018-07-301-0/+10
* Allow use of typedesc as type converters (#8409)LemonBoy2018-07-241-0/+11
* Do not crash while instantiating a generic outside a call (#8279)LemonBoy2018-07-141-0/+7
* Reset typedescMatched before paramTypesMatch (#8250)LemonBoy2018-07-091-0/+15
* make tests green againAndreas Rumpf2018-07-054-5/+5
* fix #6928; fix #7208Zahary Karadjov2018-06-161-0/+21
* fixes #7222; fixes #5595; fixes #3747Zahary Karadjov2018-06-161-0/+124
* fix #7653Zahary Karadjov2018-06-101-1/+3
* Fix type inference with static literals.LemonBoy2018-06-101-0/+12
* Bugfix: The compiler were not inserting proper downcasts for generic typesZahary Karadjov2018-05-072-1/+21
* fixes #7247Andreas Rumpf2018-02-231-6/+15
* make more tests greenAraq2018-02-102-2/+2
* better type inference for numerical types; prerequisitive for version 1Andreas Rumpf2018-02-021-0/+12
* Improved collection-to-string behavior (#6825)Fabian Keller2017-12-141-2/+2
* make tests green againAndreas Rumpf2017-11-181-0/+1
* Remove expr/stmt (#5857)Arne Döring2017-07-254-5/+5
* close #5106Zahary Karadjov2017-06-201-0/+61
* close #5756Zahary Karadjov2017-06-201-0/+30
* fix #5864Zahary Karadjov2017-06-201-0/+14
* Fix #5962Zahary Karadjov2017-06-201-0/+81
* fixes tproctypecache_falsepositive.nim test caseAraq2017-06-081-0/+17
* fix 5756Zahary Karadjov2017-04-301-0/+39
* alternative fake covariance based on convertersZahary Karadjov2017-04-282-3/+25
* more advanced fake covarianceZahary Karadjov2017-04-281-3/+48
* a simple way to simulate covariance in generic typesZahary Karadjov2017-04-281-0/+11
* fix a regrsesion in signature matching of derived ptr typesZahary Karadjov2017-04-181-0/+20
* fix a compilation error in linalgZahary Karadjov2017-04-161-0/+16
* fix #5689Zahary Karadjov2017-04-151-0/+68
* fix #5683Zahary Karadjov2017-04-141-0/+15
* fix #5643; fix #5644Zahary Karadjov2017-04-081-0/+30
* Restore the Nim's 0.14 proper handling of generic aliasesZahary Karadjov2017-04-084-0/+101
* attempt to fix #5632 typedesc typeRel regression (#5634)andri lim2017-03-311-0/+12
* attempt to fix #5621 #5615 generic ref object typeRel (#5633)andri lim2017-03-311-0/+42
* Working test cases for the sophisticated matrix library example from the manualZahary Karadjov2017-03-241-0/+29
* fixes #5597; wrong eager template instantiation in generic context (#5601)Andreas Rumpf2017-03-241-0/+13
* fixes #5241, fixes #5411 inherit from specialized generic typeRel problem (#5...andri lim2017-03-231-0/+65
* Fix generic forward declarations; fixes #4104; fixes #4908 (#5566)zah2017-03-232-10/+24
* Correct the spelling of the word 'overridden'. (#5212)mfxmfx2017-01-111-3/+3
* fix #4884Zahary Karadjov2017-01-081-0/+11
* further progress; more tests are greenAraq2016-11-281-1/+1
_a@web.de> 2013-05-03 23:51:43 +0200 committer Araq <rumpf_a@web.de> 2013-05-03 23:51:43 +0200 completed expr/stmt unification' href='/ahoang/Nim/commit/compiler/semdestruct.nim?h=devel&id=fa0a327dd6f8f18ce935f6bd8993e4af474d1231'>fa0a327dd ^
2b80d75aa ^
fa0a327dd ^



92b8fac94 ^
fa0a327dd ^
c1c6b6e50 ^
fa0a327dd ^


02533c260 ^

2b80d75aa ^
02533c260 ^



9673e4f2d ^
2b80d75aa ^
02533c260 ^
fa0a327dd ^

02533c260 ^




2b80d75aa ^
02533c260 ^
fa0a327dd ^

02533c260 ^
fa0a327dd ^
3d1c6de63 ^
fa0a327dd ^
02533c260 ^
fa0a327dd ^
02533c260 ^
fa0a327dd ^
92b8fac94 ^
fa0a327dd ^











02533c260 ^
fa0a327dd ^





02533c260 ^


fa0a327dd ^
92b8fac94 ^
02533c260 ^
fa0a327dd ^
02533c260 ^
fa0a327dd ^
























2b80d75aa ^
02533c260 ^

2b80d75aa ^
02533c260 ^
fa0a327dd ^



2b80d75aa ^
fa0a327dd ^










2b80d75aa ^
fa0a327dd ^


02533c260 ^
fa0a327dd ^







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237

 
                            







                                                   
                       

                                                  
                                                                     


                                                                      


                                                  



                                     
                                                                          

                            
                                                          

                                                        
                                          
                                   









                                                         
 






                                                           

                                                             
                                                             
                                             

                                      


                                                                          

                                                         
                                           
                                       








                                                                 



                                                                    
                   
                                                          
         

                                  
 
                             
 


                                   


















                                                                            


                                                                  
 



                                                           
                                                                      
                                      
                                                      


                                                                   

                                                             
                                                            



                                                               
                                                                            
 
                                                 

                                                                        




                                                   
 
                                  

                                                                
                                                  
                                    
                                                                          
                                        
              
         
                
                       
                                      











                                             
                                                              





                                                  


                                               
         
                                        
                
       
              
























                                                                              
 

                                                             
 
                            



                                                           
                                                                     










                                                                
 


                                             
                                             







                                               
#
#
#           The Nim Compiler
#        (c) Copyright 2013 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module implements destructors.

# included from sem.nim

# special marker values that indicates that we are
# 1) AnalyzingDestructor: currently analyzing the type for destructor
# generation (needed for recursive types)
# 2) DestructorIsTrivial: completed the analysis before and determined
# that the type has a trivial destructor
var analyzingDestructor, destructorIsTrivial: PSym
new(analyzingDestructor)
new(destructorIsTrivial)

var
  destructorName = getIdent"destroy_"
  destructorParam = getIdent"this_"
  destructorPragma = newIdentNode(getIdent"destructor", unknownLineInfo())
  rangeDestructorProc*: PSym

proc instantiateDestructor(c: PContext, typ: PType): PType

proc doDestructorStuff(c: PContext, s: PSym, n: PNode) =
  var t = s.typ.sons[1].skipTypes({tyVar})
  if t.kind == tyGenericInvocation:
    for i in 1 .. <t.sonsLen:
      if t.sons[i].kind != tyGenericParam:
        localError(n.info, errDestructorNotGenericEnough)
        return
    t = t.base
  elif t.kind == tyCompositeTypeClass:
    t = t.base
    if t.kind != tyGenericBody:
      localError(n.info, errDestructorNotGenericEnough)
      return

  t.destructor = s
  # automatically insert calls to base classes' destructors
  if n.sons[bodyPos].kind != nkEmpty:
    for i in countup(0, t.sonsLen - 1):
      # when inheriting directly from object
      # there will be a single nil son
      if t.sons[i] == nil: continue
      let destructableT = instantiateDestructor(c, t.sons[i])
      if destructableT != nil:
        n.sons[bodyPos].addSon(newNode(nkCall, t.sym.info, @[
            useSym(destructableT.destructor),
            n.sons[paramsPos][1][0]]))

proc destroyFieldOrFields(c: PContext, field: PNode, holder: PNode): PNode

proc destroySym(c: PContext, field: PSym, holder: PNode): PNode =
  let destructableT = instantiateDestructor(c, field.typ)
  if destructableT != nil:
    result = newNode(nkCall, field.info, @[
      useSym(destructableT.destructor),
      newNode(nkDotExpr, field.info, @[holder, useSym(field)])])

proc destroyCase(c: PContext, n: PNode, holder: PNode): PNode =
  var nonTrivialFields = 0
  result = newNode(nkCaseStmt, n.info, @[])
  # case x.kind
  result.addSon(newNode(nkDotExpr, n.info, @[holder, n.sons[0]]))
  for i in countup(1, n.len - 1):
    # of A, B:
    let ni = n[i]
    var caseBranch = newNode(ni.kind, ni.info, ni.sons[0..ni.len-2])

    let stmt = destroyFieldOrFields(c, ni.lastSon, holder)
    if stmt == nil:
      caseBranch.addSon(newNode(nkStmtList, ni.info, @[]))
    else:
      caseBranch.addSon(stmt)
      nonTrivialFields += stmt.len

    result.addSon(caseBranch)

  # maybe no fields were destroyed?
  if nonTrivialFields == 0:
    result = nil

proc destroyFieldOrFields(c: PContext, field: PNode, holder: PNode): PNode =
  template maybeAddLine(e: expr): stmt =
    let stmt = e
    if stmt != nil:
      if result == nil: result = newNode(nkStmtList)
      result.addSon(stmt)

  case field.kind
  of nkRecCase:
    maybeAddLine destroyCase(c, field, holder)
  of nkSym:
    maybeAddLine destroySym(c, field.sym, holder)
  of nkRecList:
    for son in field:
      maybeAddLine destroyFieldOrFields(c, son, holder)
  else:
    internalAssert false

proc generateDestructor(c: PContext, t: PType): PNode =
  ## generate a destructor for a user-defined object or tuple type
  ## returns nil if the destructor turns out to be trivial

  # XXX: This may be true for some C-imported types such as
  # Tposix_spawnattr
  if t.n == nil or t.n.sons == nil: return
  internalAssert t.n.kind == nkRecList
  let destructedObj = newIdentNode(destructorParam, unknownLineInfo())
  # call the destructods of all fields
  result = destroyFieldOrFields(c, t.n, destructedObj)
  # base classes' destructors will be automatically called by
  # semProcAux for both auto-generated and user-defined destructors

proc instantiateDestructor(c: PContext, typ: PType): PType =
  # returns nil if a variable of type `typ` doesn't require a
  # destructor. Otherwise, returns the type, which holds the
  # destructor that must be used for the varialbe.
  # The destructor is either user-defined or automatically
  # generated by the compiler in a member-wise fashion.
  var t = skipTypes(typ, {tyConst, tyMutable}).skipGenericAlias
  let typeHoldingUserDefinition = if t.kind == tyGenericInst: t.base else: t

  if typeHoldingUserDefinition.destructor != nil:
    # XXX: This is not entirely correct for recursive types, but we need
    # it temporarily to hide the "destroy is already defined" problem
    if typeHoldingUserDefinition.destructor notin
        [analyzingDestructor, destructorIsTrivial]:
      return typeHoldingUserDefinition
    else:
      return nil

  t = t.skipTypes({tyGenericInst})
  case t.kind
  of tySequence, tyArray, tyArrayConstr, tyOpenArray, tyVarargs:
    if instantiateDestructor(c, t.sons[0]) != nil:
      if rangeDestructorProc == nil:
        rangeDestructorProc = searchInScopes(c, getIdent"nimDestroyRange")
      t.destructor = rangeDestructorProc
      return t
    else:
      return nil
  of tyTuple, tyObject:
    t.destructor = analyzingDestructor
    let generated = generateDestructor(c, t)
    if generated != nil:
      internalAssert t.sym != nil
      var i = t.sym.info
      let fullDef = newNode(nkProcDef, i, @[
        newIdentNode(destructorName, i),
        emptyNode,
        emptyNode,
        newNode(nkFormalParams, i, @[
          emptyNode,
          newNode(nkIdentDefs, i, @[
            newIdentNode(destructorParam, i),
            symNodeFromType(c, makeVarType(c, t), t.sym.info),
            emptyNode]),
          ]),
        newNode(nkPragma, i, @[destructorPragma]),
        emptyNode,
        generated
        ])
      let semantizedDef = semProc(c, fullDef)
      t.destructor = semantizedDef[namePos].sym
      return t
    else:
      t.destructor = destructorIsTrivial
      return nil
  else:
    return nil

proc insertDestructors(c: PContext,
                       varSection: PNode): tuple[outer, inner: PNode] =
  # Accepts a var or let section.
  #
  # When a var section has variables with destructors
  # the var section is split up and finally blocks are inserted
  # immediately after all "destructable" vars
  #
  # In case there were no destrucable variables, the proc returns
  # (nil, nil) and the enclosing stmt-list requires no modifications.
  #
  # Otherwise, after the try blocks are created, the rest of the enclosing
  # stmt-list should be inserted in the most `inner` such block (corresponding
  # to the last variable).
  #
  # `outer` is a statement list that should replace the original var section.
  # It will include the new truncated var section followed by the outermost
  # try block.
  let totalVars = varSection.sonsLen
  for j in countup(0, totalVars - 1):
    let
      varId = varSection[j][0]
      varTyp = varId.sym.typ
      info = varId.info

    if varTyp == nil or sfGlobal in varId.sym.flags: continue
    let destructableT = instantiateDestructor(c, varTyp)

    if destructableT != nil:
      var tryStmt = newNodeI(nkTryStmt, info)

      if j < totalVars - 1:
        var remainingVars = newNodeI(varSection.kind, info)
        remainingVars.sons = varSection.sons[(j+1)..varSection.len-1]
        let (outer, inner) = insertDestructors(c, remainingVars)
        if outer != nil:
          tryStmt.addSon(outer)
          result.inner = inner
        else:
          result.inner = newNodeI(nkStmtList, info)
          result.inner.addSon(remainingVars)
          tryStmt.addSon(result.inner)
      else:
        result.inner = newNodeI(nkStmtList, info)
        tryStmt.addSon(result.inner)

      tryStmt.addSon(
        newNode(nkFinally, info, @[
          semStmt(c, newNode(nkCall, info, @[
            useSym(destructableT.destructor),
            useSym(varId.sym)]))]))

      result.outer = newNodeI(nkStmtList, info)
      varSection.sons.setLen(j+1)
      result.outer.addSon(varSection)
      result.outer.addSon(tryStmt)

      return