From 896a59fae5fcc2747aa98a8e413f4a6182c04c05 Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Tue, 19 Jan 2021 16:04:41 +0000 Subject: [PATCH] allow tasty version to be inspected --- .../dotty/tools/dotc/CompilationTests.scala | 2 +- .../tools/tasty/TastyHeaderUnpickler.scala | 46 +++++++++++++++++++ .../tools/tasty/TastyHeaderUnpickler.scala | 0 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tasty/src-bootstrapped/dotty/tools/tasty/TastyHeaderUnpickler.scala rename tasty/{src => src-non-bootstrapped}/dotty/tools/tasty/TastyHeaderUnpickler.scala (100%) diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index 0115898deb8f..67976e3368ab 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -247,7 +247,7 @@ class CompilationTests { // "-source", "3.1", // TODO: re-enable once we allow : @unchecked in pattern definitions. Right now, lots of narrowing pattern definitions fail. ))(libGroup) - val tastyCoreSources = sources(Paths.get("tasty/src")) + val tastyCoreSources = sources(Paths.get("tasty/src")) ++ sources(Paths.get("tasty/src-bootstrapped")) val tastyCore = compileList("tastyCore", tastyCoreSources, opt)(tastyCoreGroup) val compilerSources = sources(Paths.get("compiler/src")) ++ sources(Paths.get("compiler/src-bootstrapped")) diff --git a/tasty/src-bootstrapped/dotty/tools/tasty/TastyHeaderUnpickler.scala b/tasty/src-bootstrapped/dotty/tools/tasty/TastyHeaderUnpickler.scala new file mode 100644 index 000000000000..766df348becf --- /dev/null +++ b/tasty/src-bootstrapped/dotty/tools/tasty/TastyHeaderUnpickler.scala @@ -0,0 +1,46 @@ +package dotty.tools.tasty + +import java.util.UUID + +import TastyFormat.{MajorVersion, MinorVersion, header} + +sealed abstract case class TastyHeader(uuid: UUID, majorVersion: Int, minorVersion: Int) + +class TastyHeaderUnpickler(reader: TastyReader) { + import reader._ + + def this(bytes: Array[Byte]) = this(new TastyReader(bytes)) + + /** delegates to `readFullHeader`, extracting the UUID */ + def readHeader(): UUID = + readFullHeader().uuid + + /**Reads the header of a Tasty File, the returned header, `h`, + * has the following properties: + * - `h.majorVersion == TastyFormat.MajorVersion` + * - `0 <= h.minorVersion <= TastyFormat.MinorVersion` + */ + def readFullHeader(): TastyHeader = { + for (i <- 0 until header.length) + check(readByte() == header(i), "not a TASTy file") + val majorVersion = readNat() + val minorVersion = readNat() + val validVersion = ( + majorVersion == MajorVersion + && minorVersion >= 0 && minorVersion <= MinorVersion + ) + check(validVersion, + s"""TASTy signature has wrong version. + | expected: $MajorVersion.$MinorVersion + | found : ${majorVersion}.${minorVersion}""".stripMargin + ) + val uuid = new UUID(readUncompressedLong(), readUncompressedLong()) + new TastyHeader(uuid, majorVersion, minorVersion) {} + } + + def isAtEnd: Boolean = reader.isAtEnd + + private def check(cond: Boolean, msg: => String): Unit = { + if (!cond) throw new UnpickleException(msg) + } +} diff --git a/tasty/src/dotty/tools/tasty/TastyHeaderUnpickler.scala b/tasty/src-non-bootstrapped/dotty/tools/tasty/TastyHeaderUnpickler.scala similarity index 100% rename from tasty/src/dotty/tools/tasty/TastyHeaderUnpickler.scala rename to tasty/src-non-bootstrapped/dotty/tools/tasty/TastyHeaderUnpickler.scala