From 540f20a4ff5fa880f7db67b7bc3bec260cce4d92 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 24 Nov 2023 10:33:04 +0100 Subject: [PATCH] Refactor TASTy Attributes --- .../dotc/core/tasty/AttributePickler.scala | 11 +++----- .../dotc/core/tasty/AttributeUnpickler.scala | 25 +++++-------------- .../tools/dotc/core/tasty/Attributes.scala | 23 ++++++++++++++--- .../dotc/core/tasty/DottyUnpickler.scala | 2 +- .../tools/dotc/core/tasty/TastyPrinter.scala | 12 ++++++--- 5 files changed, 39 insertions(+), 34 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala index 669d41910d57..d7c268a6b4da 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala @@ -5,8 +5,6 @@ import dotty.tools.dotc.ast.{tpd, untpd} import dotty.tools.tasty.TastyBuffer import dotty.tools.tasty.TastyFormat, TastyFormat.AttributesSection -import java.nio.charset.StandardCharsets - object AttributePickler: def pickleAttributes( @@ -14,12 +12,11 @@ object AttributePickler: pickler: TastyPickler, buf: TastyBuffer ): Unit = - if attributes.scala2StandardLibrary || attributes.explicitNulls then // or any other attribute is set + if attributes.booleanTags.nonEmpty then pickler.newSection(AttributesSection, buf) - // Pickle attributes - if attributes.scala2StandardLibrary then buf.writeNat(TastyFormat.SCALA2STANDARDLIBRARYattr) - if attributes.explicitNulls then buf.writeNat(TastyFormat.EXPLICITNULLSattr) - end if + + for tag <- attributes.booleanTags do + buf.writeByte(tag) end pickleAttributes diff --git a/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala index 206b4b799ac3..5fa61ef887df 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala @@ -5,29 +5,16 @@ import scala.language.unsafeNulls import dotty.tools.tasty.{TastyFormat, TastyReader, TastyBuffer} -import java.nio.charset.StandardCharsets - class AttributeUnpickler(reader: TastyReader): import reader._ - lazy val attributeTags: List[Int] = - val listBuilder = List.newBuilder[Int] - while !isAtEnd do listBuilder += readNat() - listBuilder.result() - lazy val attributes: Attributes = { - var scala2StandardLibrary = false - var explicitNulls = false - for attributeTag <- attributeTags do - attributeTag match - case TastyFormat.SCALA2STANDARDLIBRARYattr => scala2StandardLibrary = true - case TastyFormat.EXPLICITNULLSattr => explicitNulls = true - case attribute => - assert(false, "Unexpected attribute value: " + attribute) - Attributes( - scala2StandardLibrary, - explicitNulls, - ) + val booleanTags = List.newBuilder[Int] + + while !isAtEnd do + booleanTags += readByte() + + new Attributes(booleanTags.result()) } end AttributeUnpickler diff --git a/compiler/src/dotty/tools/dotc/core/tasty/Attributes.scala b/compiler/src/dotty/tools/dotc/core/tasty/Attributes.scala index 77d2d391bd98..874767166229 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/Attributes.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/Attributes.scala @@ -1,6 +1,23 @@ package dotty.tools.dotc.core.tasty +import dotty.tools.tasty.TastyFormat + class Attributes( - val scala2StandardLibrary: Boolean, - val explicitNulls: Boolean, -) + val booleanTags: List[Int], +) { + def scala2StandardLibrary: Boolean = + booleanTags.contains(TastyFormat.SCALA2STANDARDLIBRARYattr) + def explicitNulls: Boolean = + booleanTags.contains(TastyFormat.EXPLICITNULLSattr) +} + +object Attributes: + def apply( + scala2StandardLibrary: Boolean, + explicitNulls: Boolean, + ): Attributes = + val booleanTags = List.newBuilder[Int] + if scala2StandardLibrary then booleanTags += TastyFormat.SCALA2STANDARDLIBRARYattr + if explicitNulls then booleanTags += TastyFormat.EXPLICITNULLSattr + new Attributes(booleanTags.result()) + end apply diff --git a/compiler/src/dotty/tools/dotc/core/tasty/DottyUnpickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/DottyUnpickler.scala index 02e3fc8c04e4..b4fba925df4d 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/DottyUnpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/DottyUnpickler.scala @@ -57,7 +57,7 @@ class DottyUnpickler(bytes: Array[Byte], mode: UnpickleMode = UnpickleMode.TopLe private val treeUnpickler = unpickler.unpickle(treeSectionUnpickler(posUnpicklerOpt, commentUnpicklerOpt, attributeUnpicklerOpt)).get def tastyAttributes: Attributes = - attributeUnpicklerOpt.map(_.attributes).getOrElse(Attributes(false, false)) + attributeUnpicklerOpt.map(_.attributes).getOrElse(new Attributes(booleanTags = Nil)) /** Enter all toplevel classes and objects into their scopes * @param roots a set of SymDenotations that should be overwritten by unpickling diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala index ae15421c82f3..2b5360349305 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala @@ -225,15 +225,19 @@ class TastyPrinter(bytes: Array[Byte]) { } class AttributesSectionUnpickler extends SectionUnpickler[String](AttributesSection) { - import dotty.tools.tasty.TastyFormat.attributeTagToString + import dotty.tools.tasty.TastyFormat.* + private val sb: StringBuilder = new StringBuilder def unpickle(reader: TastyReader, tastyName: NameTable): String = { + import reader.* sb.append(s" ${reader.endAddr.index - reader.currentAddr.index}") - val attributeTags = new AttributeUnpickler(reader).attributeTags + val attributes = new AttributeUnpickler(reader).attributes sb.append(s" attributes bytes:\n") - for attributeTag <- attributeTags do - sb.append(" ").append(attributeTagToString(attributeTag)).append("\n") + + for tag <- attributes.booleanTags do + sb.append(" ").append(attributeTagToString(tag)).append("\n") + sb.result } }