From f16b12c3a58bdd692089c23288a45a80f14332ab Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 23 Feb 2015 13:53:22 +0100 Subject: First version of position pickler. --- .../tools/dotc/core/pickling/PositionPickler.scala | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/dotty/tools/dotc/core/pickling/PositionPickler.scala (limited to 'src/dotty/tools/dotc/core/pickling/PositionPickler.scala') diff --git a/src/dotty/tools/dotc/core/pickling/PositionPickler.scala b/src/dotty/tools/dotc/core/pickling/PositionPickler.scala new file mode 100644 index 000000000..0cf519b23 --- /dev/null +++ b/src/dotty/tools/dotc/core/pickling/PositionPickler.scala @@ -0,0 +1,51 @@ +package dotty.tools +package dotc +package core +package pickling + +import ast.tpd._ +import PickleFormat._ +import core._ +import Contexts._, Symbols._, Types._, Names._, Constants._, Decorators._, Annotations._ +import collection.mutable +import TastyBuffer._ + +class PositionPickler(pickler: TastyPickler, addrOfTree: Tree => Option[Addr]) { + val buf = new PositionBuffer + pickler.newSection("Positions", buf) + import buf._ + + val noOp = () => () + + def traverseAll(root: Tree, recorder: PositionRecorder)(implicit ctx: Context) = { + import recorder.edge.{seq, offset} + + def elemsTraversal(xs: TraversableOnce[Any]): () => Unit = + (noOp /: xs) ((op, x) => () => seq(op, elemTraversal(x))) + + def elemTraversal(x: Any): () => Unit = () => x match { + case x: Tree @ unchecked => + if (x.pos.exists) + for (addr <- addrOfTree(x)) + recorder.record(addr, offset(x.pos)) + + val annotTraversal = x match { + case x: MemberDef => elemsTraversal(x.symbol.annotations) + case _ => noOp + } + val childrenTraversal = elemsTraversal(x.productIterator) + seq(annotTraversal, childrenTraversal) + case xs: List[_] => + elemsTraversal(xs)() + case _ => + () + } + + elemTraversal(root)() + } + + def picklePositions(root: Tree)(implicit ctx: Context) = { + traverseAll(root, startPos) + traverseAll(root, endPos) + } +} \ No newline at end of file -- cgit v1.2.3