aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/core/pickling/TreePickler.scala
blob: 8c92e2ed83b16c0b3e102f09d40835f7d8d9a7e8 (plain) (blame)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package dotty.tools
package dotc
package core
package pickling

import util.Util.{bestFit, dble}
import ast.Trees._
import PickleFormat._
import core._
import Contexts._, Symbols._, Types._, Names._, Constants._, Decorators._
import collection.mutable
import TastyBuffer._

class TreePickler(pickler: TastyPickler, picklePositions: Boolean) {
  val buf = new TreeBuffer
  pickler.newSection("ASTs", buf)
  import buf._
  import pickler.nameBuffer.nameIndex
  import ast.tpd._

  private val symRefs = new mutable.HashMap[Symbol, Addr]
  private val forwardSymRefs = new mutable.HashMap[Symbol, List[Addr]]
  private val pickledTypes = new java.util.IdentityHashMap[Type, Any] // Value type is really Addr, but that's not compatible with null

  private def withLength(op: => Unit) = {
    val lengthAddr = reserveRef(relative = true)
    op
    fillRef(lengthAddr, currentAddr, relative = true)
  }

  def registerDef(sym: Symbol) = {
    symRefs(sym) = currentAddr
    forwardSymRefs.get(sym) match {
      case Some(refs) =>
        refs.foreach(fillRef(_, currentAddr, relative = false))
        forwardSymRefs -= sym
      case None =>
    }
  }

  private def pickleName(name: Name) = writeNat(nameIndex(name).index)
  private def pickleName(name: TastyName) = writeNat(nameIndex(name).index)
  private def pickleNameAndSig(name: Name, sig: Signature) = {
    val Signature(params, result) = sig
    pickleName(TastyName.Signed(nameIndex(name), params.map(nameIndex), nameIndex(result)))
  }

  private def pickleSym(sym: Symbol) = symRefs.get(sym) match {
    case Some(label) =>
      writeRef(label)
    case None =>
      val ref = reserveRef(relative = false)
      forwardSymRefs(sym) = ref :: forwardSymRefs.getOrElse(sym, Nil)
  }

  def pickle(tree: Tree)(implicit ctx: Context) = {
    
    def pickleConstant(c: Constant): Unit = {
      def pickleNum(nonNegTag: Int, negTag: Int) = {
        val x = c.longValue
        if (x < 0) {
          writeByte(negTag)
          writeLongNat(-(x + 1))
        }
        else {
          writeByte(nonNegTag)
          writeLongNat(x)
        }
      }
      c.tag match {
        case UnitTag => 
          writeByte(UNITconst)
        case BooleanTag => 
          writeByte(if (c.booleanValue) TRUEconst else FALSEconst)
        case ByteTag => 
          pickleNum(BYTEconst, BYTEneg)
        case ShortTag => 
          pickleNum(SHORTconst, SHORTneg)
        case CharTag => 
          writeByte(CHARconst)
          writeNat(c.charValue)
        case IntTag => 
          pickleNum(INTconst, INTneg)
        case LongTag => 
          pickleNum(LONGconst, LONGneg)
        case FloatTag => 
          writeByte(FLOATconst)
          writeRaw(java.lang.Float.floatToRawIntBits(c.floatValue), 4)
        case DoubleTag =>
          writeByte(DOUBLEconst)
          writeRaw(java.lang.Double.doubleToRawLongBits(c.doubleValue), 8)
        case StringTag =>
          writeByte(STRINGconst)
          writeNat(nameIndex(c.stringValue).index)
        case NullTag => 
          writeByte(NULLconst)
        case ClazzTag =>
          writeByte(CLASSconst)
          withLength { pickleType(c.typeValue) }
        case EnumTag =>
          writeByte(ENUMconst)
          withLength { pickleType(c.symbolValue.termRef) }          
      }
    }

    def pickleType(tpe: Type): Unit = {
      val prev = pickledTypes.get(tpe)
      if (prev == null) {
        val addr = currentAddr
        pickleNewType(tpe)
        pickledTypes.put(tpe, addr)
      }
      else {
        writeByte(SHARED)
        writeRef(prev.asInstanceOf[Addr])
      }
    }
      
    def pickleNewType(tpe: Type)= tpe match {
      case ConstantType(value) => pickleConstant(value)
      case tpe: WithFixedSym =>
        if (tpe.prefix == NoPrefix) {
          writeByte(if (tpe.isType) TYPEREFdirect else TERMREFdirect)
          pickleSym(tpe.symbol)
        }
        else {
          writeByte(if (tpe.isType) TYPEREFsymbol else TERMREFsymbol)
          pickleType(tpe.prefix); pickleSym(tpe.symbol)          
        }
      case tpe: TermRefWithSignature =>
        writeByte(TERMREF)
        pickleType(tpe.prefix); pickleNameAndSig(tpe.name, tpe.signature)
      case tpe: NamedType =>
        writeByte(if (tpe.isType) TYPEREF else TERMREF)
        pickleType(tpe.prefix); pickleName(tpe.name)
      case tpe: ThisType =>
        writeByte(THIS)
        pickleType(tpe.tref)
      case tpe: SuperType =>
        writeByte(SUPERtype)
        withLength { pickleType(tpe.thistpe); pickleType(tpe.supertpe)}
      case tpe: SkolemType =>
        writeByte(SKOLEMtype)
        withLength { pickleType(tpe.underlying) }
      case tpe: RefinedType =>
        val args = tpe.argInfos(interpolate = false)
        if (args.isEmpty) {
          writeByte(REFINEDtype)
          withLength { pickleName(tpe.refinedName); pickleType(tpe.refinedInfo) }
        }
        else {
          writeByte(APPLIEDtype)
          withLength { pickleType(tpe.withoutArgs(args)); args.foreach(pickleType) }
        }
      case tpe: TypeAlias =>
        writeByte(TYPEALIAS)
        withLength { pickleType(tpe.alias) }
      case tpe: TypeBounds =>
        writeByte(TYPEBOUNDS)
        withLength { pickleType(tpe.lo); pickleType(tpe.hi) }
      case tpe: AnnotatedType =>
        writeByte(ANNOTATED)
        withLength { pickleTree(tpe.annot.tree); pickleType(tpe.tpe) }
      case tpe: AndOrType =>
        writeByte(if (tpe.isAnd) ANDtype else ORtype)
        withLength { pickleType(tpe.tp1); pickleType(tpe.tp2) }
      case tpe: ExprType =>
        writeByte(BYNAMEtype)
        withLength { pickleType(tpe.underlying) }
      case NoType =>
        writeByte(NOTYPE)
//      case NoPrefix =>    // not sure we need this!
//        writeByte(NOPREFIX)
    }
    
    def pickleTpt(tpt: Tree): Unit = pickleType(tpt.tpe) // TODO correlate with original when generating positions
    
    def pickleTreeIfNonEmpty(tree: Tree): Unit = 
      if (!tree.isEmpty) pickleTree(tree)

    def pickleTree(tree: Tree): Unit = tree match {
      case Ident(_) | This(_) => 
        pickleType(tree.tpe)
      case Select(qual, name) => 
        writeByte(SELECT)
        val sig = tree.tpe.signature
        if (sig == Signature.NotAMethod) pickleName(name)
        else pickleNameAndSig(name, sig)
      case Apply(fun, args) =>
        writeByte(APPLY)
        withLength {
          pickleTree(fun)
          args.foreach(pickleTree)
        }
      case TypeApply(fun, args) =>
        writeByte(TYPEAPPLY)
        withLength {
          pickleTree(fun)
          args.foreach(pickleTree)
        }
      case Literal(const) =>
        pickleConstant(const)
      case Super(qual, mix) =>
        writeByte(SUPER)
        withLength { 
          pickleTree(qual);
          if (!mix.isEmpty) {
            val SuperType(_, mixinType) = tree.tpe
            pickleType(mixinType)
          }
        }
      case New(tpt) =>
        writeByte(NEW)
        withLength { pickleTpt(tpt) }
      case Pair(left, right) =>
        writeByte(PAIR)
        withLength { pickleTree(left); pickleTree(right) }
      case Typed(expr, tpt) =>
        writeByte(TYPED)
        withLength { pickleTree(expr); pickleTpt(tpt) }
      case NamedArg(name, arg) =>
        writeByte(NAMEDARG)
        withLength { pickleName(name); pickleTree(arg) }
      case Assign(lhs, rhs) =>
        writeByte(ASSIGN)
        withLength { pickleTree(lhs); pickleTree(rhs) }
      case Block(stats, expr) =>
        writeByte(BLOCK)
        withLength { pickleTree(expr); stats.foreach(pickleTree) }
      case If(cond, thenp, elsep) =>
        writeByte(IF)
        withLength{ pickleTree(cond); pickleTree(thenp); pickleTree(elsep) }
      case Closure(env, meth, tpt) => 
        writeByte(CLOSURE)
        withLength{ pickleTree(meth); pickleTpt(tpt); env.foreach(pickleTree) }
      case Match(selector, cases) =>
        writeByte(MATCH)
        withLength { pickleTree(selector); cases.foreach(pickleTree) }
      case CaseDef(pat, guard, rhs) =>
        writeByte(CASEDEF)
        withLength { pickleTree(pat); pickleTree(guard); pickleTree(rhs) }
      case Return(expr, from) =>
        writeByte(RETURN)
        withLength { pickleSym(from.symbol); pickleTreeIfNonEmpty(expr) }
      case Try(block, cases, finalizer) =>
        writeByte(TRY)
        withLength { pickleTree(block); cases.foreach(pickleTree); pickleTreeIfNonEmpty(finalizer) }
      case Throw(expr) =>
        writeByte(THROW)
        withLength { pickleTree(expr) }
      case SeqLiteral(elems) =>
        writeByte(if (tree.isInstanceOf[JavaSeqLiteral]) JSEQLITERAL else SEQLITERAL)
        withLength { elems.foreach(pickleTree) }
      case TypeTree(original) =>
        pickleTpt(tree)
      case Bind(name, body) =>
        registerDef(tree.symbol)
        writeByte(BIND)
        withLength { pickleName(name); pickleType(tree.symbol.info); pickleTree(body) }
      case Alternative(alts) =>
        writeByte(ALTERNATIVE)
        withLength { alts.foreach(pickleTree) }
      case UnApply(fun, implicits, patterns) =>
        writeByte(UNAPPLY)
        withLength { 
          pickleTree(fun)
          for (implicitArg <- implicits) {
            writeByte(IMPLICITARG)
            withLength { pickleTree(implicitArg) }
          }
          patterns.foreach(pickleTree) 
        }
      case tree: ValDef =>
        pickleDef(VALDEF, tree.symbol, tree.rhs)
      case tree: DefDef =>
        def pickleParams = {
          for (tparam <- tree.tparams) pickleDef(TYPEPARAM, tparam.symbol, EmptyTree)
          for (vparams <- tree.vparamss) {
            writeByte(PARAMS)
            withLength {
              for (vparam <- vparams) pickleDef(PARAM, vparam.symbol, EmptyTree)
            }
          }          
        }
        pickleDef(DEFDEF, tree.symbol, tree.rhs, pickleParams)
      case tree: TypeDef =>
        pickleDef(TYPEDEF, tree.symbol, tree.rhs)
      case tree: Template =>
        writeByte(TEMPLATE)
        withLength {
          tree.parents.foreach(pickleTree)
          if (!tree.self.isEmpty)
            pickleDef(PARAM, tree.self.symbol, EmptyTree)
          pickleTreeIfNonEmpty(tree.constr)
          tree.body.foreach(pickleTree)
        }
      case Import(expr, selectors) =>
        writeByte(IMPORT)
        withLength {
          pickleTree(expr)
          selectors foreach {
            case Pair(Ident(from), Ident(to)) => 
              writeByte(RENAMED)
              withLength { pickleName(from); pickleName(to) }
            case Ident(name) =>
              writeByte(IMPORTED)
              withLength { pickleName(name) }
          }
        }
      case PackageDef(pid, stats) =>
        writeByte(PACKAGE)
        withLength { pickleType(pid.tpe); stats.foreach(pickleTree) }
      case Annotated(annot, arg) =>
        writeByte(ANNOTATED)
        withLength { pickleTree(annot); pickleTree(arg) }
    }
    
    def pickleDef(tag: Int, sym: Symbol, rhs: Tree, pickleParams: => Unit = ()) = {
      registerDef(sym)
      writeByte(tag)
      withLength {
        pickleName(sym.name)
        pickleParams
        if (tag != TYPEDEF) pickleType(sym.info.finalResultType)
        if (tag != PARAM && tag != TYPEPARAM) pickleTree(rhs)
        pickleModifiers(sym)
      }
    }

    def pickleModifiers(sym: Symbol): Unit = {
      import Flags._
      val flags = sym.flags
      val privateWithin = sym.privateWithin
      if (privateWithin.exists) {
        writeByte(if (flags is Protected) PROTECTEDqualified else PRIVATEqualified)
        pickleSym(privateWithin)
      }
      if (flags is Private) writeByte(PRIVATE)
      if (flags is Protected) if (!privateWithin.exists) writeByte(PROTECTED)
      if (flags is Final) writeByte(FINAL)
      if (flags is Case) writeByte(CASE)
      if (flags is Override) writeByte(OVERRIDE)
      if (flags is Inline) writeByte(INLINE)
      if (flags is JavaStatic) writeByte(STATIC)
      if (flags is Module) writeByte(MODULE)
      if (flags is Local) writeByte(LOCAL)
      if (flags is Synthetic) writeByte(SYNTHETIC)
      if (flags is Artifact) writeByte(ARTIFACT)
      if (flags is Scala2x) writeByte(SCALA2X) 
      if (sym.isTerm) {
        if (flags is Implicit) writeByte(IMPLICIT)
        if (flags is Lazy) writeByte(LAZY)
        if (flags is AbsOverride) writeByte(ABSOVERRIDE)
        if (flags is Mutable) writeByte(MUTABLE)
        if (flags is Accessor) writeByte(FIELDaccessor)
        if (flags is ParamAccessor) writeByte(PARAMaccessor)
        if (flags is CaseAccessor) writeByte(CASEaccessor)  
        if (flags is DefaultParameterized) writeByte(DEFAULTparameterized)
        if (flags is DefaultInit) writeByte(DEFAULTinit)
      } else {
        if (flags is Sealed) writeByte(SEALED)
        if (flags is Abstract) writeByte(ABSTRACT)      
        if (flags is Covariant) writeByte(COVARIANT)
        if (flags is Contravariant) writeByte(CONTRAVARIANT)
      }
      sym.annotations.foreach(ann => pickleTree(ann.tree))
    }

    pickleTree(tree)
  }  
}