summary refs log tree commit diff stats
path: root/compiler/semdata.nim
Commit message (Expand)AuthorAgeFilesLines
...
* | added 'sig' feature; removed tfShared support in the compilerAndreas Rumpf2016-02-281-0/+3
* | added experimental .this pragmaAndreas Rumpf2016-02-281-11/+2
* | first implementation of the new lambda-lifting pass; barely anything worksAndreas Rumpf2015-12-261-1/+1
|/
* renamed inCompilesContext to compilesContextId; added test case for #3313Araq2015-09-181-1/+1
* fixes #3313Araq2015-09-181-0/+1
* next steps in binding [] properlyAraq2015-09-131-1/+2
* fixes #2585Araq2015-04-221-4/+6
* fixes #2505, fixes #1853, fixes #2522Araq2015-04-201-0/+3
* fixes #2481Araq2015-04-101-1/+2
* first implementation of overloading of '='; missing: rewriting let/var sectionsAraq2015-04-061-2/+7
* implemented a[^1] notationAraq2015-03-261-27/+28
* cleaned up some testsAraq2015-02-101-0/+1
* nimsuggest improvementsAraq2015-01-301-0/+3
* fixes #1915Araq2015-01-121-0/+1
* fix #1049Zahary Karadjov2015-01-021-1/+1
* implemented 'experimental' switchAraq2014-12-051-0/+5
* nicer error messages (untested)Araq2014-10-261-2/+1
* deepCopy is instantiated when its corresponding type is instantiatedAraq2014-09-261-0/+3
* updated the compiler to use the new symbol namesAraq2014-08-281-3/+3
* Nimrod renamed to NimAraq2014-08-281-1/+1
* Merge pull request #1281 from Araq/new_spawnAndreas Rumpf2014-06-161-0/+1
|\
| * pi test compiles, but crashes randomlyAraq2014-06-011-0/+1
* | Fixed #1172 (for real)Clay Sweetser2014-06-011-2/+2
|/
* removed flawed thread analysis passAraq2014-04-201-2/+0
* propagate semExpr flags in macro/template expansionZahary Karadjov2014-04-061-1/+1
* fixed parens around getSysType argumentsJimmie Houchin2014-03-251-1/+1
* reference implementation of a vector swizzle libraryZahary Karadjov2014-03-201-0/+21
* fix #715 againZahary Karadjov2014-03-161-1/+1
* fix tsemistaticZahary Karadjov2014-03-111-0/+3
* split the inline and closure iterators into different symbol kinds for easier...Zahary Karadjov2014-03-061-1/+1
* quite messy implementation of generic lambdas, needs reworking; fixes #715Zahary Karadjov2014-02-171-0/+1
* fix some regressions caused by tyTypeDesc[tyNone]Zahary Karadjov2014-02-151-1/+0
* fixes #797; generic procs can be used in places expecting matching concrete p...Zahary Karadjov2014-02-111-0/+2
* tyTypeDesc and tyRange always have 1 child; this might be tyNone but it is re...Araq2014-02-051-0/+1
* fixes #844Araq2014-02-021-1/+1
* progress towards fixing tgenericshardcasesZahary Karadjov2014-01-061-5/+10
* introduce tyFromExpr; fixes #618Zahary Karadjov2014-01-041-0/+4
* Merge branch 'upstream' into develZahary Karadjov2013-12-291-1/+19
|\
| * integrate the logic of fixupProcType into ReplaceTypeVarsZahary Karadjov2013-12-291-8/+6
| * better integration of tyStatic into typeRelZahary Karadjov2013-12-251-0/+5
| * wip type class reforms (the compiler bootstraps fine)Zahary Karadjov2013-12-251-0/+15
* | case consistency: cs:partial bootstraps on windowsAraq2013-12-291-2/+2
* | case consistency: next stepsAraq2013-12-291-3/+3
* | case consistency part 4Araq2013-12-271-5/+5
* | case consistency part 1Araq2013-12-271-16/+16
* | Merge branch 'master' into vm2Araq2013-12-051-1/+3
|\|
| * simple unit test and better documentation for the user defined type classesZahary Karadjov2013-11-171-1/+2
| * improvements for the `is` operator; implemented the type lifting rule in user...Zahary Karadjov2013-11-171-0/+1
* | compiler bootstraps with new VMAraq2013-10-151-1/+1
|/
* improvements for TR macrosAraq2013-09-171-0/+1
piler/semasgn.nim?h=devel&id=c68c42a237c09af4a23fcf207aa51f9f12f5e917'>^
3bcafb1c3 ^
594674797 ^
58d5f9679 ^







594674797 ^
8780d25e0 ^
594674797 ^
58d5f9679 ^



















78f371c03 ^




46efaf294 ^
78f371c03 ^
58d5f9679 ^
78f371c03 ^
58d5f9679 ^



78f371c03 ^


46efaf294 ^
78f371c03 ^




594674797 ^
78f371c03 ^












































594674797 ^
b47d9b7b9 ^
78f371c03 ^
e879da579 ^
78f371c03 ^
e6c5622aa ^
9911d375b ^
78f371c03 ^
05e39cf6a ^
78f371c03 ^







594674797 ^
78f371c03 ^


3a13706d7 ^

78f371c03 ^
594674797 ^
78f371c03 ^
594674797 ^

78f371c03 ^
594674797 ^


78f371c03 ^
594674797 ^


78f371c03 ^
594674797 ^

6f7b891bd ^
594674797 ^
f5c3eb6a2 ^
b3de34548 ^
594674797 ^
8cd5f1f8f ^
e879da579 ^
78f371c03 ^
f7f3a25be ^
78f371c03 ^








594674797 ^
78f371c03 ^




58d5f9679 ^

594674797 ^

b7a1cad4a ^
58d5f9679 ^
78f371c03 ^
58d5f9679 ^






78f371c03 ^









58d5f9679 ^


1a0032eb6 ^


58d5f9679 ^




78f371c03 ^
78f371c03 ^






78f371c03 ^

e6c5622aa ^
78f371c03 ^

58d5f9679 ^
78f371c03 ^



45d74f408 ^
58d5f9679 ^
45d74f408 ^

45d74f408 ^

4f633dde6 ^
58d5f9679 ^
35c8b5e3b ^


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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326


                            
                                         




                                                   

                                                                              






                                      
                         


                      
 
                                                              

                                                             
















                                                                               
 
                                                         
             
           

                                                             

                      

                                    

                                               
                                                 
                        

                                      
                                                                            

                                 
                        


                                                          
                          

                                                                          
               
                                                    
       
                        
 










                                                                   
                                   

                           






                                        
                                           

                                                    
              
 







                                                             
                                                    
                                           
 



















                                                                     




                                                                               
                                              
                               
                                         
                   



                                                               


                       
                                              




                                                              
                                  












































                                                                             
             
                                     
                                                                          
                                    
                               
                         
                                                              
                              
                                     







                                                                    
         


                                                  

                                                                 
                                                            
             
                                 

                                                             
                                 


                                                                             
                                            


                                                                   
                                   

                                                             
                                                              
                                                                               
                                                          
                                                
                                                                              
                                    
                                             
                                          
                                                                             








                                                                           
 




                                             

                                                             

                 
         
               
                                       






                                                          









                                                          


                                


                                                                             




                                                
 






                                              

                                                                        
                                                        

                       
                                               



                                                           
 
                                                                  

                                                                  

                                                         
                                                   
                                                                              


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

## This module implements lifting for assignments. Later versions of this code
## will be able to also lift ``=deepCopy`` and ``=destroy``.

# included from sem.nim

type
  TLiftCtx = object
    c: PContext
    info: TLineInfo # for construction
    kind: TTypeAttachedOp
    fn: PSym
    asgnForType: PType
    recurse: bool

proc liftBodyAux(c: var TLiftCtx; t: PType; body, x, y: PNode)
proc liftBody(c: PContext; typ: PType; kind: TTypeAttachedOp;
              info: TLineInfo): PSym {.discardable.}

proc at(a, i: PNode, elemType: PType): PNode =
  result = newNodeI(nkBracketExpr, a.info, 2)
  result.sons[0] = a
  result.sons[1] = i
  result.typ = elemType

proc liftBodyTup(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  for i in 0 .. <t.len:
    let lit = lowerings.newIntLit(i)
    liftBodyAux(c, t.sons[i], body, x.at(lit, t.sons[i]), y.at(lit, t.sons[i]))

proc dotField(x: PNode, f: PSym): PNode =
  result = newNodeI(nkDotExpr, x.info, 2)
  result.sons[0] = x
  result.sons[1] = newSymNode(f, x.info)
  result.typ = f.typ

proc liftBodyObj(c: var TLiftCtx; n, body, x, y: PNode) =
  case n.kind
  of nkSym:
    let f = n.sym
    liftBodyAux(c, f.typ, body, x.dotField(f), y.dotField(f))
  of nkNilLit: discard
  of nkRecCase:
    # copy the selector:
    liftBodyObj(c, n[0], body, x, y)
    # we need to generate a case statement:
    var caseStmt = newNodeI(nkCaseStmt, c.info)
    # XXX generate 'if' that checks same branches
    # generate selector:
    var access = dotField(x, n[0].sym)
    caseStmt.add(access)
    # copy the branches over, but replace the fields with the for loop body:
    for i in 1 .. <n.len:
      var branch = copyTree(n[i])
      let L = branch.len
      branch.sons[L-1] = newNodeI(nkStmtList, c.info)

      liftBodyObj(c, n[i].lastSon, branch.sons[L-1], x, y)
      caseStmt.add(branch)
    body.add(caseStmt)
    localError(c.info, "cannot lift assignment operator to 'case' object")
  of nkRecList:
    for t in items(n): liftBodyObj(c, t, body, x, y)
  else:
    illFormedAstLocal(n)

proc genAddr(c: PContext; x: PNode): PNode =
  if x.kind == nkHiddenDeref:
    checkSonsLen(x, 1)
    result = x.sons[0]
  else:
    result = newNodeIT(nkHiddenAddr, x.info, makeVarType(c, x.typ))
    addSon(result, x)

proc newAsgnCall(c: PContext; op: PSym; x, y: PNode): PNode =
  if sfError in op.flags:
    localError(x.info, errWrongSymbolX, op.name.s)
  result = newNodeI(nkCall, x.info)
  result.add newSymNode(op)
  result.add genAddr(c, x)
  result.add y

proc newAsgnStmt(le, ri: PNode): PNode =
  result = newNodeI(nkAsgn, le.info, 2)
  result.sons[0] = le
  result.sons[1] = ri

proc newOpCall(op: PSym; x: PNode): PNode =
  result = newNodeIT(nkCall, x.info, op.typ.sons[0])
  result.add(newSymNode(op))
  result.add x

proc destructorCall(c: PContext; op: PSym; x: PNode): PNode =
  result = newNodeIT(nkCall, x.info, op.typ.sons[0])
  result.add(newSymNode(op))
  if newDestructors:
    result.add genAddr(c, x)
  else:
    result.add x

proc newDeepCopyCall(op: PSym; x, y: PNode): PNode =
  result = newAsgnStmt(x, newOpCall(op, y))

proc considerAsgnOrSink(c: var TLiftCtx; t: PType; body, x, y: PNode;
                        field: PSym): bool =
  if tfHasAsgn in t.flags:
    var op: PSym
    if sameType(t, c.asgnForType):
      # generate recursive call:
      if c.recurse:
        op = c.fn
      else:
        c.recurse = true
        return false
    else:
      op = field
      if op == nil:
        op = liftBody(c.c, t, c.kind, c.info)
    markUsed(c.info, op, c.c.graph.usageSym)
    styleCheckUse(c.info, op)
    body.add newAsgnCall(c.c, op, x, y)
    result = true

proc considerOverloadedOp(c: var TLiftCtx; t: PType; body, x, y: PNode): bool =
  case c.kind
  of attachedDestructor:
    let op = t.destructor
    if op != nil:
      markUsed(c.info, op, c.c.graph.usageSym)
      styleCheckUse(c.info, op)
      body.add destructorCall(c.c, op, x)
      result = true
  of attachedAsgn:
    result = considerAsgnOrSink(c, t, body, x, y, t.assignment)
  of attachedSink:
    result = considerAsgnOrSink(c, t, body, x, y, t.sink)
  of attachedDeepCopy:
    let op = t.deepCopy
    if op != nil:
      markUsed(c.info, op, c.c.graph.usageSym)
      styleCheckUse(c.info, op)
      body.add newDeepCopyCall(op, x, y)
      result = true

proc defaultOp(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  if c.kind != attachedDestructor:
    body.add newAsgnStmt(x, y)

proc addVar(father, v, value: PNode) =
  var vpart = newNodeI(nkIdentDefs, v.info, 3)
  vpart.sons[0] = v
  vpart.sons[1] = ast.emptyNode
  vpart.sons[2] = value
  addSon(father, vpart)

proc declareCounter(c: var TLiftCtx; body: PNode; first: BiggestInt): PNode =
  var temp = newSym(skTemp, getIdent(lowerings.genPrefix), c.fn, c.info)
  temp.typ = getSysType(tyInt)
  incl(temp.flags, sfFromGeneric)

  var v = newNodeI(nkVarSection, c.info)
  result = newSymNode(temp)
  v.addVar(result, lowerings.newIntLit(first))
  body.add v

proc genBuiltin(magic: TMagic; name: string; i: PNode): PNode =
  result = newNodeI(nkCall, i.info)
  result.add createMagic(name, magic).newSymNode
  result.add i

proc genWhileLoop(c: var TLiftCtx; i, dest: PNode): PNode =
  result = newNodeI(nkWhileStmt, c.info, 2)
  let cmp = genBuiltin(mLeI, "<=", i)
  cmp.add genHigh(dest)
  cmp.typ = getSysType(tyBool)
  result.sons[0] = cmp
  result.sons[1] = newNodeI(nkStmtList, c.info)

proc addIncStmt(body, i: PNode) =
  let incCall = genBuiltin(mInc, "inc", i)
  incCall.add lowerings.newIntLit(1)
  body.add incCall

proc newSeqCall(c: PContext; x, y: PNode): PNode =
  # don't call genAddr(c, x) here:
  result = genBuiltin(mNewSeq, "newSeq", x)
  let lenCall = genBuiltin(mLengthSeq, "len", y)
  lenCall.typ = getSysType(tyInt)
  result.add lenCall

proc liftBodyAux(c: var TLiftCtx; t: PType; body, x, y: PNode) =
  case t.kind
  of tyNone, tyEmpty, tyVoid: discard
  of tyPointer, tySet, tyBool, tyChar, tyEnum, tyInt..tyUInt64, tyCString,
      tyPtr, tyString, tyRef, tyOpt:
    defaultOp(c, t, body, x, y)
  of tyArray, tySequence:
    if {tfHasAsgn, tfUncheckedArray} * t.flags == {tfHasAsgn}:
      if t.kind == tySequence:
        # XXX add 'nil' handling here
        body.add newSeqCall(c.c, x, y)
      let i = declareCounter(c, body, firstOrd(t))
      let whileLoop = genWhileLoop(c, i, x)
      let elemType = t.lastSon
      liftBodyAux(c, elemType, whileLoop.sons[1], x.at(i, elemType),
                                                  y.at(i, elemType))
      addIncStmt(whileLoop.sons[1], i)
      body.add whileLoop
    else:
      defaultOp(c, t, body, x, y)
  of tyObject, tyDistinct:
    if not considerOverloadedOp(c, t, body, x, y):
      if t.sons[0] != nil:
        liftBodyAux(c, t.sons[0].skipTypes(skipPtrs), body, x, y)
      if t.kind == tyObject: liftBodyObj(c, t.n, body, x, y)
  of tyTuple:
    liftBodyTup(c, t, body, x, y)
  of tyProc:
    if t.callConv != ccClosure or c.kind != attachedDeepCopy:
      defaultOp(c, t, body, x, y)
    else:
      # a big problem is that we don't know the enviroment's type here, so we
      # have to go through some indirection; we delegate this to the codegen:
      let call = newNodeI(nkCall, c.info, 2)
      call.typ = t
      call.sons[0] = newSymNode(createMagic("deepCopy", mDeepCopy))
      call.sons[1] = y
      body.add newAsgnStmt(x, call)
  of tyVarargs, tyOpenArray:
    localError(c.info, errGenerated, "cannot copy openArray")
  of tyFromExpr, tyProxy, tyBuiltInTypeClass, tyUserTypeClass,
     tyUserTypeClassInst, tyCompositeTypeClass, tyAnd, tyOr, tyNot, tyAnything,
     tyGenericParam, tyGenericBody, tyNil, tyExpr, tyStmt,
     tyTypeDesc, tyGenericInvocation, tyForward:
    internalError(c.info, "assignment requested for type: " & typeToString(t))
  of tyOrdinal, tyRange, tyInferred,
     tyGenericInst, tyStatic, tyVar, tyAlias:
    liftBodyAux(c, lastSon(t), body, x, y)
  of tyUnused, tyOptAsRef, tyUnused1, tyUnused2: internalError("liftBodyAux")

proc newProcType(info: TLineInfo; owner: PSym): PType =
  result = newType(tyProc, owner)
  result.n = newNodeI(nkFormalParams, info)
  rawAddSon(result, nil) # return type
  # result.n[0] used to be `nkType`, but now it's `nkEffectList` because
  # the effects are now stored in there too ... this is a bit hacky, but as
  # usual we desperately try to save memory:
  addSon(result.n, newNodeI(nkEffectList, info))

proc addParam(procType: PType; param: PSym) =
  param.position = procType.len-1
  addSon(procType.n, newSymNode(param))
  rawAddSon(procType, param.typ)

proc liftBody(c: PContext; typ: PType; kind: TTypeAttachedOp;
              info: TLineInfo): PSym {.discardable.} =
  var a: TLiftCtx
  a.info = info
  a.c = c
  a.kind = kind
  let body = newNodeI(nkStmtList, info)
  let procname = case kind
                 of attachedAsgn: getIdent":Asgn"
                 of attachedSink: getIdent":Sink"
                 of attachedDeepCopy: getIdent":DeepCopy"
                 of attachedDestructor: getIdent":Destroy"

  result = newSym(skProc, procname, typ.owner, info)
  a.fn = result
  a.asgnForType = typ

  let dest = newSym(skParam, getIdent"dest", result, info)
  let src = newSym(skParam, getIdent"src", result, info)
  dest.typ = makeVarType(c, typ)
  src.typ = typ

  result.typ = newProcType(info, typ.owner)
  result.typ.addParam dest
  if kind != attachedDestructor:
    result.typ.addParam src

  liftBodyAux(a, typ, body, newSymNode(dest).newDeref, newSymNode(src))
  # recursion is handled explicitly, do not register the type based operation
  # before 'liftBodyAux':
  case kind
  of attachedAsgn: typ.assignment = result
  of attachedSink: typ.sink = result
  of attachedDeepCopy: typ.deepCopy = result
  of attachedDestructor: typ.destructor = result

  var n = newNodeI(nkProcDef, info, bodyPos+1)
  for i in 0 .. < n.len: n.sons[i] = emptyNode
  n.sons[namePos] = newSymNode(result)
  n.sons[paramsPos] = result.typ.n
  n.sons[bodyPos] = body
  result.ast = n


proc getAsgnOrLiftBody(c: PContext; typ: PType; info: TLineInfo): PSym =
  let t = typ.skipTypes({tyGenericInst, tyVar, tyAlias})
  result = t.assignment
  if result.isNil:
    result = liftBody(c, t, attachedAsgn, info)

proc overloadedAsgn(c: PContext; dest, src: PNode): PNode =
  let a = getAsgnOrLiftBody(c, dest.typ, dest.info)
  result = newAsgnCall(c, a, dest, src)

proc liftTypeBoundOps*(c: PContext; typ: PType; info: TLineInfo) =
  ## In the semantic pass this is called in strategic places
  ## to ensure we lift assignment, destructors and moves properly.
  ## The later 'destroyer' pass depends on it.
  if not newDestructors or not hasDestructor(typ): return
  let typ = typ.skipTypes({tyGenericInst, tyAlias})
  # we generate the destructor first so that other operators can depend on it:
  if typ.destructor == nil: liftBody(c, typ, attachedDestructor, info)
  if typ.assignment == nil: liftBody(c, typ, attachedAsgn, info)
  if typ.sink == nil: liftBody(c, typ, attachedSink, info)