aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/core/pickling/TastyReader.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2015-02-10 12:39:15 +0100
committerDmitry Petrashko <dmitry.petrashko@gmail.com>2015-03-18 11:09:43 +0100
commit1c5f3b75f7e70e8608d6be442087857bd4a6b2cc (patch)
tree26a197f56bfbd0a0ffa819cf68a730abd88a6260 /src/dotty/tools/dotc/core/pickling/TastyReader.scala
parent41922c14bf1a45a3dcf7afca7719e0be84c2c29a (diff)
downloaddotty-1c5f3b75f7e70e8608d6be442087857bd4a6b2cc.tar.gz
dotty-1c5f3b75f7e70e8608d6be442087857bd4a6b2cc.tar.bz2
dotty-1c5f3b75f7e70e8608d6be442087857bd4a6b2cc.zip
Add TASTY readers and printers for TASTy info.
So far printing is the only reader, ie. deserializer. Numerous bugfixes to make first tests work.
Diffstat (limited to 'src/dotty/tools/dotc/core/pickling/TastyReader.scala')
-rw-r--r--src/dotty/tools/dotc/core/pickling/TastyReader.scala78
1 files changed, 78 insertions, 0 deletions
diff --git a/src/dotty/tools/dotc/core/pickling/TastyReader.scala b/src/dotty/tools/dotc/core/pickling/TastyReader.scala
new file mode 100644
index 000000000..659eb8977
--- /dev/null
+++ b/src/dotty/tools/dotc/core/pickling/TastyReader.scala
@@ -0,0 +1,78 @@
+package dotty.tools
+package dotc
+package core
+package pickling
+
+
+import TastyBuffer._
+import TastyName.NameRef
+import collection.mutable
+
+/** A byte array bufferfer that can be filled with bytes or natural numbers in TASTY format,
+ * and that supports reading and patching addresses represented as natural numbers.
+ */
+class TastyReader(val bytes: Array[Byte], val from: Addr, val end: Addr) {
+
+ def this(bytes: Array[Byte]) = this(bytes, Addr(0), Addr(bytes.length))
+
+ private var bp: Int = from.index
+
+ def currentAddr: Addr = Addr(bp)
+
+ def atEnd: Boolean = bp == end.index
+
+ /** Read a byte of data. */
+ def readByte(): Int = {
+ val result = bytes(bp) & 0xff
+ bp += 1
+ result
+ }
+
+ /** Read the next `n` bytes of `data`. */
+ def readBytes(n: Int): Array[Byte] = {
+ val result = new Array[Byte](n)
+ Array.copy(bytes, bp, result, 0, n)
+ bp += n
+ result
+ }
+
+ /** Read a natural number fitting in an Int in big endian format, base 128.
+ * All but the last digits have bit 0x80 set.
+ */
+ def readNat(): Int = readLongNat.toInt
+
+ /** Read a natural number fitting in a Long in big endian format, base 128.
+ * All but the last digits have bit 0x80 set.
+ */
+ def readLongNat(): Long = {
+ var b = 0L
+ var x = 0L
+ do {
+ b = bytes(bp)
+ x = (x << 7) | (b & 0x7f)
+ bp += 1
+ } while ((b & 0x80) == 0)
+ x
+ }
+
+ /** Read `nbytes` bytes in big endian format into a Long */
+ def readRaw(nbytes: Int): Unit = {
+ def recur(x: Long, n: Int): Long =
+ if (n == 0) x else recur((x << 8) | (readByte & 0xff), n - 1)
+ recur(0, nbytes)
+ }
+
+ def readNameRef() = NameRef(readNat())
+
+ def readEnd(): Addr = Addr(readNat() + bp)
+
+ def skipTo(addr: Addr): Unit =
+ bp = addr.index
+
+ def until[T](end: Addr)(op: => T): List[T] = {
+ val buf = new mutable.ListBuffer[T]
+ while (bp < end.index) buf += op
+ assert(bp == end.index)
+ buf.toList
+ }
+}