summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/nsc/symtab/classfile/UnPickler.scala
blob: 1fc87fa3a7edcede2093bcfec8c0dd4d6de3279b (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/* NSC -- new Scala compiler
 * Copyright 2005-2009 LAMP/EPFL
 * @author  Martin Odersky
 */
// $Id$

package scala.tools.nsc.symtab.classfile

import java.io.IOException
import java.lang.{Float, Double}

import scala.tools.nsc.util.{Position, NoPosition}
import scala.io.UTF8Codec

import Flags._
import PickleFormat._
import collection.mutable.{HashMap, ListBuffer}

/** This abstract class implements ..
 *
 *  @author Martin Odersky
 *  @version 1.0
 */
abstract class UnPickler {
  val global: Global
  import global._

  /** Unpickle symbol table information descending from a class and/or module root
   *  from an array of bytes.
   *  @param bytes      bytearray from which we unpickle
   *  @param offset     offset from which unpickling starts
   *  @param classroot  the top-level class which is unpickled, or NoSymbol if unapplicable
   *  @param moduleroot the top-level module which is unpickled, or NoSymbol if unapplicable
   *  @param filename   filename associated with bytearray, only used for error messages
   */
  def unpickle(bytes: Array[Byte], offset: Int, classRoot: Symbol, moduleRoot: Symbol, filename: String) {
    try {
      new UnPickle(bytes, offset, classRoot, moduleRoot, filename)
    } catch {
      case ex: IOException =>
        throw ex
      case ex: Throwable =>
        if (settings.debug.value) ex.printStackTrace()
        throw new RuntimeException("error reading Scala signature of "+filename+": "+ex.getMessage())
    }
  }

  private class UnPickle(bytes: Array[Byte], offset: Int, classRoot: Symbol, moduleRoot: Symbol, filename: String) extends PickleBuffer(bytes, offset, -1) {
    if (settings.debug.value) global.log("unpickle " + classRoot + " and " + moduleRoot)
    checkVersion(filename)

    /** A map from entry numbers to array offsets */
    private val index = createIndex

    /** A map from entry numbers to symbols, types, or annotations */
    private val entries = new Array[AnyRef](index.length)

    /** A map from symbols to their associated `decls' scopes */
    private val symScopes = new HashMap[Symbol, Scope]

    for (i <- 0 until index.length) {
      if (isSymbolEntry(i)) { at(i, readSymbol); {} }
      else if (isAnnotationEntry(i)) { at(i, readAnnotation); {} }
    }

    if (settings.debug.value) global.log("unpickled " + classRoot + ":" + classRoot.rawInfo + ", " + moduleRoot + ":" + moduleRoot.rawInfo);//debug

    private def checkVersion(filename: String) {
      val major = readNat()
      val minor = readNat()

// remove the portion below, between "cut here", before releasing the first 2.8 beta

//---cut here---

/*
      // transiently, use this bit as long as stability fails.
      if (major != 4 && major != 5)
*/

      // once stability is restored, use the following bit instead:
      if (major == 4) { // !!! temporarily accept 4 as version.
        println("WARNING: old class format, please recompile "+filename)
      } else

//---cut here---
      if (major != MajorVersion || minor > MinorVersion)
        throw new IOException("Scala signature " + classRoot.name +
                              " has wrong version\n expected: " +
                              MajorVersion + "." + MinorVersion +
                              "\n found: " + major + "." + minor +
                              " in "+filename)
    }

    /** The `decls' scope associated with given symbol */
    private def symScope(sym: Symbol, isTemp : Boolean) = symScopes.get(sym) match {
      case None =>
        val s = if (isTemp) newTempScope
                else if (sym.isClass || sym.isModuleClass || sym.isModule) newClassScope(sym);
                else newScope

        symScopes(sym) = s; s
      case Some(s) => s
    }
    private def symScope(sym : Symbol) : Scope = symScope(sym, false)

    /** Does entry represent an (internal) symbol */
    private def isSymbolEntry(i: Int): Boolean = {
      val tag = bytes(index(i)) % PosOffset
      (firstSymTag <= tag && tag <= lastSymTag &&
       (tag != CLASSsym || !isRefinementSymbolEntry(i)))
    }

    /** Does entry represent an (internal or external) symbol */
    private def isSymbolRef(i: Int): Boolean = {
      val tag = bytes(index(i)) % PosOffset
      (firstSymTag <= tag && tag <= lastExtSymTag)
    }

    /** Does entry represent a name? */
    private def isNameEntry(i: Int): Boolean = {
      val tag = bytes(index(i))
      tag == TERMname || tag == TYPEname
    }

    /** Does entry represent a symbol attribute? */
    private def isAnnotationEntry(i: Int): Boolean = {
      val tag = bytes(index(i))
      tag == ATTRIBUTE || tag == CHILDREN
    }

    /** Does entry represent a refinement symbol?
     *  pre: Entry is a class symbol
     */
    private def isRefinementSymbolEntry(i: Int): Boolean = {
      val savedIndex = readIndex
      readIndex = index(i)
      val tag = readByte()
      if (tag % PosOffset != CLASSsym) assert(false)
      readNat(); // read length
      if (tag > PosOffset) readNat(); // read position
      val result = readNameRef() == nme.REFINE_CLASS_NAME.toTypeName
      readIndex = savedIndex
      result
    }

    /** If entry at <code>i</code> is undefined, define it by performing
     *  operation <code>op</code> with <code>readIndex at start of i'th
     *  entry. Restore <code>readIndex</code> afterwards.
     */
    private def at[T <: AnyRef](i: Int, op: () => T): T = {
      var r = entries(i)
      if (r eq null) {
        val savedIndex = readIndex
        readIndex = index(i)
        r = op()
        assert(entries(i) eq null, entries(i))
        entries(i) = r
        readIndex = savedIndex
      }
      r.asInstanceOf[T]
    }

    /** Read a name */
    private def readName(): Name = {
      val tag = readByte()
      val len = readNat()
      tag match {
        case TERMname => newTermName(bytes, readIndex, len)
        case TYPEname => newTypeName(bytes, readIndex, len)
        case _ => errorBadSignature("bad name tag: " + tag)
      }
    }

    /** Read a symbol */
    private def readSymbol(): Symbol = {
      val tag = readByte()
      val end = readNat() + readIndex
      var sym: Symbol = NoSymbol
      tag match {
        case EXTref | EXTMODCLASSref =>
          val name = readNameRef()
          val owner = if (readIndex == end) definitions.RootClass else readSymbolRef()
          sym = if (name.toTermName == nme.ROOT) definitions.RootClass
                else if (name == nme.ROOTPKG) definitions.RootPackage
                else if (tag == EXTref) owner.info.decl(name)
                else owner.info.decl(name).moduleClass
          if (sym == NoSymbol) {
            errorBadSignature(
              "reference " + (if (name.isTypeName) "type " else "value ") +
              name.decode + " of " + owner + " refers to nonexisting symbol.")
          }
        case NONEsym =>
          sym = NoSymbol
        case _ =>
          val unusedPos : Int = {
            if (tag > PosOffset) readNat
            else -1
          }
          var defaultGetter: Symbol = NoSymbol
          var nameref = readNat()
          if (tag == VALsym && isSymbolRef(nameref)) {
            defaultGetter = at(nameref, readSymbol)
            nameref = readNat()
          }
          val name = at(nameref, readName)
          val owner = readSymbolRef()
          val flags = pickledToRawFlags(readLongNat())
          var privateWithin: Symbol = NoSymbol
          var inforef = readNat()
          if (isSymbolRef(inforef)) {
            privateWithin = at(inforef, readSymbol)
            inforef = readNat()
          }
          (tag % PosOffset) match {
            case TYPEsym =>
              sym = owner.newAbstractType(NoPosition, name)
            case ALIASsym =>
              sym = owner.newAliasType(NoPosition, name)
            case CLASSsym =>
              sym =
                if (name == classRoot.name && owner == classRoot.owner)
                  (if ((flags & MODULE) != 0) moduleRoot.moduleClass
                   else classRoot)
                else
                  if ((flags & MODULE) != 0) owner.newModuleClass(NoPosition, name)
                  else owner.newClass(NoPosition, name)
              if (readIndex != end) sym.typeOfThis = new LazyTypeRef(readNat())
            case MODULEsym =>
              val clazz = at(inforef, readType).typeSymbol
              sym =
                if (name == moduleRoot.name && owner == moduleRoot.owner) moduleRoot
                else {
                  assert(clazz.isInstanceOf[ModuleClassSymbol], clazz)
                  val mclazz = clazz.asInstanceOf[ModuleClassSymbol]
                  val m = owner.newModule(NoPosition, name, mclazz)
                  mclazz.setSourceModule(m)
                  m
                }
            case VALsym =>
              sym = if (name == moduleRoot.name && owner == moduleRoot.owner) moduleRoot.resetFlag(MODULE)
                    else owner.newValue(NoPosition, name)
              sym.defaultGetter = defaultGetter
            case _ =>
              errorBadSignature("bad symbol tag: " + tag)
          }
          sym.setFlag(flags.toLong & PickledFlags)
          sym.privateWithin = privateWithin
          if (readIndex != end) assert(sym hasFlag (SUPERACCESSOR | PARAMACCESSOR), sym)
          if (sym hasFlag SUPERACCESSOR) assert(readIndex != end)
          sym.setInfo(
            if (readIndex != end) new LazyTypeRefAndAlias(inforef, readNat())
            else new LazyTypeRef(inforef))
          if (sym.owner.isClass && sym != classRoot && sym != moduleRoot &&
              !sym.isModuleClass && !sym.isRefinementClass && !sym.isTypeParameter && !sym.isExistential)
            symScope(sym.owner) enter sym
      }
      sym
    }

    /** To avoid cutting and pasting between METHODtpe and IMPLICITMETHODtpe */
    private def readMethodParams(isImplicit: Boolean, end: Int): List[Symbol] = {
      if (readIndex == end)
        return Nil

      val index = readNat()
      if (isSymbolRef(index))
        at(index, readSymbol) :: until(end, readSymbolRef)
      else {
        val formals =
          if (isImplicit) until(end, readTypeRef)
          else at(index, readType) :: until(end, readTypeRef)

        // @LUC TODO the owner should be the method symbol, and newSyntheticValueParams
        // should only be called once, not separately for each parameter list
        val dummyMethod = new TermSymbol(NoSymbol, NoPosition, "unPickler$dummy")
        dummyMethod.newSyntheticValueParams(formals)
      }
    }

    /** Read a type */
    private def readType(): Type = {
      val tag = readByte()
      val end = readNat() + readIndex
      tag match {
        case NOtpe =>
          NoType
        case NOPREFIXtpe =>
          NoPrefix
        case THIStpe =>
          mkThisType(readSymbolRef())
        case SINGLEtpe =>
          singleType(readTypeRef(), readSymbolRef())
        case CONSTANTtpe =>
          mkConstantType(readConstantRef())
        case TYPEREFtpe =>
          val pre = readTypeRef()
          val sym = readSymbolRef()
          var args = until(end, readTypeRef)
          rawTypeRef(pre, sym, args)
        case TYPEBOUNDStpe =>
          mkTypeBounds(readTypeRef(), readTypeRef())
        case REFINEDtpe =>
          val clazz = readSymbolRef()
/*
          val ps = until(end, readTypeRef)
          val dcls = symScope(clazz)
          new RefinedType(ps, dcls) { override def symbol = clazz }
*/
         new RefinedType(until(end, readTypeRef), symScope(clazz, true)) {
           override def typeSymbol = clazz
          }
        case CLASSINFOtpe =>
          val clazz = readSymbolRef()
          ClassInfoType(until(end, readTypeRef), symScope(clazz), clazz)
        case METHODtpe =>
          val restpe = readTypeRef()
          // compatibility with old format. TODO replace by "until(end, readSymbolRef)"
          val params = readMethodParams(false, end)
          MethodType(params, restpe)
        case IMPLICITMETHODtpe =>
          val restpe = readTypeRef()
          val params = readMethodParams(true, end)
          ImplicitMethodType(params, restpe)
        case POLYtpe =>
          val restpe = readTypeRef()
          PolyType(until(end, readSymbolRef), restpe)
        case EXISTENTIALtpe =>
          val restpe = readTypeRef()
          ExistentialType(until(end, readSymbolRef), restpe)
        case ANNOTATEDtpe | ANNOTATEDWSELFtpe =>
          val tp = readTypeRef()
	  val selfsym = if (tag == ANNOTATEDWSELFtpe) readSymbolRef()
                        else NoSymbol
          val attribs = until(end, readTreeAttribRef)
          if (settings.selfInAnnots.value || (selfsym eq NoSymbol))
            AnnotatedType(attribs, tp, selfsym)
          else
            tp // drop annotations with a self symbol unless
               // -Yself-in-annots is on
        case DEBRUIJNINDEXtpe =>
          DeBruijnIndex(readNat(), readNat())
        case _ =>
          errorBadSignature("bad type tag: " + tag)
      }
    }

    /** Read a constant */
    private def readConstant(): Constant = {
      val tag = readByte()
      val len = readNat()
      tag match {
        case LITERALunit    => Constant(())
        case LITERALboolean => Constant(if (readLong(len) == 0) false else true)
        case LITERALbyte    => Constant(readLong(len).asInstanceOf[Byte])
        case LITERALshort   => Constant(readLong(len).asInstanceOf[Short])
        case LITERALchar    => Constant(readLong(len).asInstanceOf[Char])
        case LITERALint     => Constant(readLong(len).asInstanceOf[Int])
        case LITERALlong    => Constant(readLong(len))
        case LITERALfloat   => Constant(Float.intBitsToFloat(readLong(len).asInstanceOf[Int]))
        case LITERALdouble  => Constant(Double.longBitsToDouble(readLong(len)))
        case LITERALstring  => Constant(readNameRef().toString())
        case LITERALnull    => Constant(null)
        case LITERALclass   => Constant(readTypeRef())
        case _              => errorBadSignature("bad constant tag: " + tag)
      }
    }

    /** Read an annotation argument.  It can use either Constant's or
     *  Tree's for its arguments.  If a reflect tree is seen, it
     *  prints a warning and returns an empty tree.
     */
    private def readAnnotationArg(): AnnotationArgument = {
      if (peekByte() == REFLTREE) {
        reflectAnnotationWarning()
	new AnnotationArgument(EmptyTree)
      } else if (peekByte() == TREE) {
	val tree = readTree()
	new AnnotationArgument(tree)
      } else {
        val const = readConstant()
        new AnnotationArgument(const)
      }
    }

    /** Read an annotation abstract syntax tree. */
    private def readAnnotationTree(): Annotation = {
      val tag = readByte()
      val end = readNat() + readIndex
      val tpe = readTypeRef()
      val constr = readTreeRef()
      val elements = until(end, readTreeRef)
      Annotation(constr, elements).setType(tpe)
    }

    /** Read an attribute and as a side effect store it into
     *  the symbol it requests. */
    private def readAnnotation(): AnyRef = {
      val tag = readByte()
      val end = readNat() + readIndex
      val target = readSymbolRef()
      if (tag == ATTRIBUTE) {
        val attrType = readTypeRef()
        val args = new ListBuffer[AnnotationArgument]
        val assocs = new ListBuffer[(Name, AnnotationArgument)]
        while (readIndex != end) {
          val argref = readNat()
          if (isNameEntry(argref))
            assocs += ((at(argref, readName), readAnnotationArgRef))
          else
            args += at(argref, readAnnotationArg)
        }
        val attr = AnnotationInfo(attrType, args.toList, assocs.toList)
        target.attributes = attr :: target.attributes
      } else if (tag == CHILDREN) {
        while (readIndex != end) target addChild readSymbolRef()
      }
      null
    }

    /** Read an annotation and return it. */
    private def readTreeAttrib(): AnnotationInfo = {
      val tag = readByte()
      if(tag != ANNOTINFO)
        errorBadSignature("tree-based annotation expected (" + tag + ")")
      val end = readNat() + readIndex

      val atp = readTypeRef()
      val numargs = readNat()
      val args = times(numargs, readAnnotationArgRef)
      val assocs =
        until(end, {() =>
          val name = readNameRef()
          val tree = readAnnotationArgRef()
          (name,tree)})
      AnnotationInfo(atp, args, assocs)
    }

    /* Read an abstract syntax tree */
    private def readTree(): Tree = {
      val outerTag = readByte()
      if (outerTag != TREE)
	errorBadSignature("tree expected (" + outerTag + ")")
      val end = readNat() + readIndex
      val tag = readByte()
      val tpe =
	if (tag != EMPTYtree)
	  readTypeRef()
	else
	  NoType

      tag match {
	case EMPTYtree =>
	  EmptyTree

	case PACKAGEtree =>
	  val symbol = readSymbolRef()
	  val name = readNameRef()
	  val stats = until(end, readTreeRef)

	  PackageDef(name, stats) setType tpe

	case CLASStree =>
	  val symbol = readSymbolRef()
          val mods = readModifiersRef()
	  val name = readNameRef()
	  val impl = readTemplateRef()
	  val tparams = until(end, readTypeDefRef)
	  (ClassDef(mods, name, tparams, impl).
	   setSymbol(symbol).
	   setType(tpe))

	case MODULEtree =>
	  val symbol = readSymbolRef()
	  val mods = readModifiersRef()
	  val name = readNameRef()
	  val impl = readTemplateRef()
	  (ModuleDef(mods, name, impl).
	   setSymbol(symbol).
	   setType(tpe))

	case VALDEFtree =>
	  val symbol = readSymbolRef()
	  val mods = readModifiersRef()
	  val name = readNameRef()
	  val tpt = readTreeRef()
	  val rhs = readTreeRef()

	  (ValDef(mods, name, tpt, rhs).
	   setSymbol(symbol).
	   setType(tpe))

	case DEFDEFtree =>
	  val symbol = readSymbolRef()
	  val mods = readModifiersRef()
	  val name = readNameRef()
	  val numTparams = readNat()
	  val tparams = times(numTparams, readTypeDefRef)
	  val numVparamss = readNat
	  val vparamss = times(numVparamss, () => {
	    val len = readNat()
	    times(len, readValDefRef)})
	  val tpt = readTreeRef()
	  val rhs = readTreeRef()

	  (DefDef(mods, name, tparams, vparamss, tpt, rhs).
  	   setSymbol(symbol).
	   setType(tpe))

	case TYPEDEFtree =>
          val symbol = readSymbolRef()
	  val mods = readModifiersRef()
	  val name = readNameRef()
	  val rhs = readTreeRef()
	  val tparams = until(end, readTypeDefRef)

	  (TypeDef(mods, name, tparams, rhs).
  	   setSymbol(symbol).
	   setType(tpe))


	case LABELtree =>
	  val symbol = readSymbolRef()
	  val name = readNameRef()
	  val rhs = readTreeRef()
	  val params = until(end, readIdentRef)
          (LabelDef(name, params, rhs).
  	   setSymbol(symbol).
	   setType(tpe))

	case IMPORTtree =>
          val symbol = readSymbolRef()
	  val expr = readTreeRef()
 	  val selectors = until(end, () => {
	    val from = readNameRef()
	    val to = readNameRef()
	    (from, to)
	  })
	  (Import(expr, selectors).
  	   setSymbol(symbol).
	   setType(tpe))

	case ANNOTATIONtree =>
	  val constr = readTreeRef()
	  val elements = until(end, readTreeRef)
	  (Annotation(constr, elements).setType(tpe))

	case DOCDEFtree =>
	  val comment = readConstantRef match {
	    case Constant(com: String) => com
	    case other =>
	      errorBadSignature("Document comment not a string (" + other + ")")
	  }
	  val definition = readTreeRef()
	  (DocDef(comment, definition).setType(tpe))

	case TEMPLATEtree =>
	  val symbol = readSymbolRef()
          val numParents = readNat()
	  val parents = times(numParents, readTreeRef)
	  val self = readValDefRef()
	  val body = until(end, readTreeRef)

	  (Template(parents, self, body).
	   setSymbol(symbol).
	   setType(tpe))

	case BLOCKtree =>
	  val expr = readTreeRef()
	  val stats = until(end, readTreeRef)
	  Block(stats, expr).setType(tpe)

	case CASEtree =>
	  val pat = readTreeRef()
	  val guard = readTreeRef()
	  val body = readTreeRef()
	  CaseDef(pat, guard, body).setType(tpe)

	case SEQUENCEtree =>
	  val trees = until(end, readTreeRef)
	  Sequence(trees).setType(tpe)

	case ALTERNATIVEtree =>
	  val trees = until(end, readTreeRef)
	  Alternative(trees).setType(tpe)

	case STARtree =>
	  val elem = readTreeRef()
	  Star(elem).setType(tpe)

	case BINDtree =>
	  val symbol = readSymbolRef()
	  val name = readNameRef()
	  val body = readTreeRef()
	  (Bind(name, body).
	   setSymbol(symbol).
	   setType(tpe))

	case UNAPPLYtree =>
	  val fun = readTreeRef()
	  val args = until(end, readTreeRef)
          (UnApply(fun: Tree, args).setType(tpe))

	case ARRAYVALUEtree =>
	  val elemtpt = readTreeRef()
	  val trees = until(end, readTreeRef)
	  (ArrayValue(elemtpt, trees).setType(tpe))

	case FUNCTIONtree =>
	  val symbol = readSymbolRef()
	  val body = readTreeRef()
	  val vparams = until(end, readValDefRef)
	  (Function(vparams, body).
	   setSymbol(symbol).
	   setType(tpe))

	case ASSIGNtree =>
          val lhs = readTreeRef()
          val rhs = readTreeRef()
	  Assign(lhs, rhs).setType(tpe)

	case IFtree =>
          val cond = readTreeRef()
          val thenp = readTreeRef()
          val elsep = readTreeRef()
	  If(cond, thenp, elsep).setType(tpe)

	case MATCHtree =>
          val selector = readTreeRef()
          val cases = until(end, readCaseDefRef)
	  Match(selector, cases).setType(tpe)

	case RETURNtree =>
	  val symbol = readSymbolRef()
	  val expr = readTreeRef()
	  (Return(expr).
	   setSymbol(symbol).
	   setType(tpe))

	case TREtree =>
          val block = readTreeRef()
	  val finalizer = readTreeRef()
	  val catches = until(end, readCaseDefRef)
	  Try(block, catches, finalizer).setType(tpe)

	case THROWtree =>
	  val expr = readTreeRef()
	  Throw(expr).setType(tpe)

	case NEWtree =>
	  val tpt = readTreeRef()
	  New(tpt).setType(tpe)

	case TYPEDtree =>
	  val expr = readTreeRef()
	  val tpt = readTreeRef()
          Typed(expr, tpt).setType(tpe)

	case TYPEAPPLYtree =>
	  val fun = readTreeRef()
	  val args = until(end, readTreeRef)
	  TypeApply(fun, args).setType(tpe)

	case APPLYtree =>
	  val fun = readTreeRef()
          val args = until(end, readTreeRef)
	  Apply(fun, args).setType(tpe)


	case APPLYDYNAMICtree =>
	  val symbol = readSymbolRef()
	  val qual = readTreeRef()
          val args = until(end, readTreeRef)
	  ApplyDynamic(qual, args).setSymbol(symbol).setType(tpe)

	case SUPERtree =>
	  val symbol = readSymbolRef()
	  val qual = readNameRef()
	  val mix = readNameRef()
	  Super(qual, mix).setSymbol(symbol).setType(tpe)

        case THIStree =>
	  val symbol = readSymbolRef()
	  val qual = readNameRef()
	  This(qual).setSymbol(symbol).setType(tpe)

        case SELECTtree =>
	  val symbol = readSymbolRef()
	  val qualifier = readTreeRef()
	  val selector = readNameRef()
	  Select(qualifier, selector).setSymbol(symbol).setType(tpe)

	case IDENTtree =>
	  val symbol = readSymbolRef()
	  val name = readNameRef()
	  Ident(name).setSymbol(symbol).setType(tpe)

	case LITERALtree =>
	  val value = readConstantRef()
	  Literal(value).setType(tpe)

	case TYPEtree =>
	  TypeTree().setType(tpe)

	case ANNOTATEDtree =>
          val annot = readAnnotationTreeRef()
          val arg = readTreeRef()
	  Annotated(annot, arg).setType(tpe)

	case SINGLETONTYPEtree =>
	  val ref = readTreeRef()
	  SingletonTypeTree(ref).setType(tpe)

	case SELECTFROMTYPEtree =>
          val qualifier = readTreeRef()
          val selector = readNameRef()
	  SelectFromTypeTree(qualifier, selector).setType(tpe)

	case COMPOUNDTYPEtree =>
	  val templ = readTemplateRef()
	  CompoundTypeTree(templ: Template).setType(tpe)

	case APPLIEDTYPEtree =>
	  val tpt = readTreeRef()
          val args = until(end, readTreeRef)
	  AppliedTypeTree(tpt, args).setType(tpe)

	case TYPEBOUNDStree =>
	  val lo = readTreeRef()
	  val hi = readTreeRef()
	  TypeBoundsTree(lo, hi).setType(tpe)

	case EXISTENTIALTYPEtree =>
	  val tpt = readTreeRef()
	  val whereClauses = until(end, readTreeRef)
	  ExistentialTypeTree(tpt, whereClauses).setType(tpe)

	case _ =>
          errorBadSignature("unknown tree type (" + tag + ")")
      }
    }

    def readModifiers(): Modifiers = {
      val tag = readNat()
      if (tag != MODIFIERS)
	errorBadSignature("expected a modifiers tag (" + tag + ")")
      val end = readNat() + readIndex
      val pflagsHi = readNat()
      val pflagsLo = readNat()
      val pflags = (pflagsHi.toLong << 32) + pflagsLo
      val flags = pickledToRawFlags(pflags)
      val privateWithin = readNameRef()
      val annotations = until(end, readAnnotationTreeRef)
      Modifiers(flags, privateWithin, annotations)
    }

    /* Read a reference to a pickled item */
    private def readNameRef(): Name = at(readNat(), readName)
    private def readSymbolRef(): Symbol = at(readNat(), readSymbol)
    private def readTypeRef(): Type = at(readNat(), readType)
    private def readConstantRef(): Constant = at(readNat(), readConstant)
    private def readAnnotationArgRef(): AnnotationArgument =
      at(readNat(), readAnnotationArg)
    private def readTreeAttribRef(): AnnotationInfo =
      at(readNat(), readTreeAttrib)
    private def readAnnotationTreeRef(): Annotation =
      at(readNat(), readAnnotationTree)
    private def readModifiersRef(): Modifiers =
      at(readNat(), readModifiers)
    private def readTreeRef(): Tree =
      at(readNat(), readTree)

    private def readTemplateRef(): Template =
      readTreeRef() match {
	case templ:Template => templ
	case other =>
          errorBadSignature("expected a template (" + other + ")")
      }
    private def readCaseDefRef(): CaseDef =
      readTreeRef() match {
	case tree:CaseDef => tree
	case other =>
          errorBadSignature("expected a case def (" + other + ")")
      }
    private def readValDefRef(): ValDef =
      readTreeRef() match {
	case tree:ValDef => tree
	case other =>
          errorBadSignature("expected a ValDef (" + other + ")")
      }
    private def readIdentRef(): Ident =
      readTreeRef() match {
	case tree:Ident => tree
	case other =>
          errorBadSignature("expected an Ident (" + other + ")")
      }
    private def readTypeDefRef(): TypeDef =
      readTreeRef() match {
	case tree:TypeDef => tree
	case other =>
          errorBadSignature("expected an TypeDef (" + other + ")")
      }


    private def errorBadSignature(msg: String) =
      throw new RuntimeException("malformed Scala signature of " + classRoot.name + " at " + readIndex + "; " + msg)

    private var printedReflectAnnotationWarning = false
    private def reflectAnnotationWarning() {
      if (!printedReflectAnnotationWarning) {
	global.warning(
	  "warning: dropping a legacy format annotation in " + classRoot.name)
	printedReflectAnnotationWarning = true
      }
    }

    private class LazyTypeRef(i: Int) extends LazyType {
      private val definedAtRunId = currentRunId
      // In IDE, captures class files dependencies so they can be reloaded when their dependencies change.
      private val ideHook = unpickleIDEHook
      override def complete(sym: Symbol) : Unit = {
        val tp = ideHook(at(i, readType))
        sym setInfo tp
        if (currentRunId != definedAtRunId) sym.setInfo(adaptToNewRunMap(tp))
      }
      override def load(sym: Symbol) { complete(sym) }
    }

    private class LazyTypeRefAndAlias(i: Int, j: Int) extends LazyTypeRef(i) {
      override def complete(sym: Symbol) {
        super.complete(sym)
        var alias = at(j, readSymbol)
        if (alias hasFlag OVERLOADED)
          alias = alias suchThat (alt => sym.tpe =:= sym.owner.thisType.memberType(alt))
        sym.asInstanceOf[TermSymbol].setAlias(alias)
      }
    }
  }
}