diff options
author | Martin Odersky <odersky@gmail.com> | 2015-02-10 12:39:15 +0100 |
---|---|---|
committer | Dmitry Petrashko <dmitry.petrashko@gmail.com> | 2015-03-18 11:09:43 +0100 |
commit | 1c5f3b75f7e70e8608d6be442087857bd4a6b2cc (patch) | |
tree | 26a197f56bfbd0a0ffa819cf68a730abd88a6260 /src/dotty/tools/dotc/core/pickling/TastyPrinter.scala | |
parent | 41922c14bf1a45a3dcf7afca7719e0be84c2c29a (diff) | |
download | dotty-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/TastyPrinter.scala')
-rw-r--r-- | src/dotty/tools/dotc/core/pickling/TastyPrinter.scala | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/dotty/tools/dotc/core/pickling/TastyPrinter.scala b/src/dotty/tools/dotc/core/pickling/TastyPrinter.scala new file mode 100644 index 000000000..3e583beb0 --- /dev/null +++ b/src/dotty/tools/dotc/core/pickling/TastyPrinter.scala @@ -0,0 +1,107 @@ +package dotty.tools.dotc +package core +package pickling + +import Contexts._, Decorators._ +import printing.Texts._ +import TastyName._ +import TastyUnpickler._ + +class TastyPrinter(bytes: Array[Byte])(implicit ctx: Context) { + + val reader = new TastyReader(bytes) + val unpickler = new TastyUnpickler(reader) + import unpickler.{tastyName, unpickled} + + def nameToString(name: TastyName): String = name match { + case Simple(name) => name.toString + case Qualified(qual, name) => nameRefToString(qual) + "." + nameRefToString(name) + case Signed(original, params, result) => + i"${nameRefToString(original)}@${params.map(nameRefToString)}%,%:${nameRefToString(result)}" + case Expanded(original) => nameRefToString(original) + "/EXPANDED" + case ModuleClass(original) => nameRefToString(original) + "/MODULECLASS" + case SuperAccessor(accessed) => nameRefToString(accessed) + "/SUPERACCESSOR" + case DefaultGetter(meth, num) => nameRefToString(meth) + "/DEFAULTGETTER" + num + } + + def nameRefToString(ref: NameRef): String = nameToString(tastyName(ref)) + + def printNames() = + for ((name, idx) <- tastyName.contents.zipWithIndex) + println(f"$idx%4d: " + nameToString(name)) + + def printContents(): Unit = { + println("Names:") + printNames() + println("Trees:") + unpickled(new TreeUnpickler) + } + + class TreeUnpickler extends SectionUnpickler[Text]("ASTs") { + import PickleFormat._ + def unpickle(reader: TastyReader, tastyName: TastyName.Table): Text = { + import reader._ + val sb = new StringBuilder(s"${reader.end.index - reader.from.index} bytes of AST:") + var indent = 0 + def newLine() = print(f"\n ${currentAddr.index - from.index}%5d:" + " " * indent) + def printNat() = print(" " + readNat()) + def printName() = { + val idx = readNat() + print(" ") ;print(idx); print("["); print(nameRefToString(NameRef(idx))); print("]") + } + def printTree(): Unit = { + newLine() + val tag = readByte() + print(" ");print(astTagToString(tag)) + indent += 2 + if (tag >= firstLengthTreeTag) { + val len = readNat() + print(s"($len)") + val end = currentAddr + len + def printTrees() = until(end)(printTree()) + tag match { + case IMPORTED => + printName() + case RENAMED => + printName(); printName() + case VALDEF | DEFDEF | TYPEDEF | TYPEPARAM | PARAM | NAMEDARG | BIND | REFINEDtype => + printName(); printTrees() + case RETURN => + printNat(); printTrees() + case METHODtype | POLYtype => + printTree() + until(end) { printName(); printTree() } + case PARAMtype => + printNat(); printNat() + case _ => + printTrees() + } + if (currentAddr != end) { + println(s"incomplete read, current = $currentAddr, end = $end") + skipTo(currentAddr) + } + } + else if (tag >= firstTreeNatTreeTag) { + printTree() + newLine() + tag match { + case SELECT | TERMREF | TYPEREF => printName() + case _ => printNat() + } + } + else if (tag >= firstNatTreeTag) + tag match { + case TERMREFstatic | TYPEREFstatic | STRINGconst => printName() + case _ => printNat() + } + indent -= 2 + } + println(s"base = $currentAddr") + while (!atEnd) { + printTree() + newLine() + } + sb.toString + } + } +}
\ No newline at end of file |