From e8a39f049f9b4e7004457989e37c04f5d8cc26a6 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 26 Oct 2023 17:22:04 +0200 Subject: [PATCH] Use tags directly to store attributes --- .../dotc/core/tasty/AttributePickler.scala | 19 ++++++++---------- .../dotc/core/tasty/AttributeUnpickler.scala | 20 ++++++++----------- .../tools/dotc/core/tasty/TastyPrinter.scala | 6 +++--- .../tools/dotc/core/tasty/TreeUnpickler.scala | 6 +++--- .../dotty/tools/dotc/transform/Pickler.scala | 9 ++++----- tasty/src/dotty/tools/tasty/TastyFormat.scala | 6 ++---- 6 files changed, 28 insertions(+), 38 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala index d91e2fb82d37..8945c145e032 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala @@ -10,18 +10,15 @@ import java.nio.charset.StandardCharsets object AttributePickler: def pickleAttributes( - attributes: List[String], + scala2StandardLibrary: Boolean, pickler: TastyPickler, - buf: TastyBuffer): Unit = - if attributes != Nil then - pickler.newSection(AttributesSection, buf) - for attribute <- attributes do - val bytes = attribute.getBytes(StandardCharsets.UTF_8).nn - val length = bytes.length - assert("[a-zA-Z0-9]+".r.matches(attribute), s"Malformed attribute: $attribute\n. Attribute must match [a-zA-Z0-9]+") - buf.writeNat(TastyFormat.FLAGattr) - buf.writeNat(length) - buf.writeBytes(bytes, length) + buf: TastyBuffer + ): Unit = + if scala2StandardLibrary then // or any other attribute is set + pickler.newSection(AttributesSection, buf) + // Pickle attributes + if scala2StandardLibrary then buf.writeNat(TastyFormat.SCALA2STANDARDLIBRARYattr) + end pickleAttributes end AttributePickler diff --git a/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala index 1dc3792c3807..19ab18eae90c 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala @@ -10,20 +10,16 @@ import java.nio.charset.StandardCharsets class AttributeUnpickler(reader: TastyReader): import reader._ - private[tasty] lazy val attributes: List[String] = { - val attributesBuilder = List.newBuilder[String] + lazy val scala2StandardLibrary = { + var scala2StandardLibrary = false while (!isAtEnd) { - val kind = readNat() - assert(kind == TastyFormat.FLAGattr, "Malformed attribute kind") - val length = readNat() - val bytes = readBytes(length) - val attribute = new String(bytes, StandardCharsets.UTF_8) - assert("[a-zA-Z0-9]+".r.matches(attribute), s"Malformed attribute: $attribute\n. Attribute must match [a-zA-Z0-9]+") - attributesBuilder += attribute + readNat() match + case TastyFormat.SCALA2STANDARDLIBRARYattr => + scala2StandardLibrary = true + case attribute => + assert(false, "Unexpected attribute value: " + attribute) } - attributesBuilder.result() + scala2StandardLibrary } - def scala2Stdlib: Boolean = attributes.contains(TastyFormat.Scala2StandardLibraryAttribute) - end AttributeUnpickler diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala index 7f15831709b5..eb627c900e71 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala @@ -230,10 +230,10 @@ class TastyPrinter(bytes: Array[Byte]) { def unpickle(reader: TastyReader, tastyName: NameTable): String = { sb.append(s" ${reader.endAddr.index - reader.currentAddr.index}") - val attributes = new AttributeUnpickler(reader).attributes + val attributeUnpickler = new AttributeUnpickler(reader) sb.append(s" attributes bytes:\n") - for attribute <- attributes do - sb.append(" ").append(attribute).append("\n") + if attributeUnpickler.scala2StandardLibrary then + sb.append(" SCALA2STANDARDLIBRARYattr\n") sb.result } } diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala index b740c7b3a708..185762f0e8c0 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala @@ -99,8 +99,8 @@ class TreeUnpickler(reader: TastyReader, /** Was unpickled class compiled with capture checks? */ private var withCaptureChecks: Boolean = false - private val unpicklingScala2Lib = - attributeUnpicklerOpt.exists(_.scala2Stdlib) + private val unpicklingScala2Library = + attributeUnpicklerOpt.exists(_.scala2StandardLibrary) private def registerSym(addr: Addr, sym: Symbol) = symAtAddr(addr) = sym @@ -614,7 +614,7 @@ class TreeUnpickler(reader: TastyReader, val rhsIsEmpty = nothingButMods(end) if (!rhsIsEmpty) skipTree() val (givenFlags0, annotFns, privateWithin) = readModifiers(end) - val givenFlags = if isClass && unpicklingScala2Lib then givenFlags0 | Scala2x | Scala2Tasty else givenFlags0 + val givenFlags = if isClass && unpicklingScala2Library then givenFlags0 | Scala2x | Scala2Tasty else givenFlags0 pickling.println(i"creating symbol $name at $start with flags ${givenFlags.flagsString}, isAbsType = $isAbsType, $ttag") val flags = normalizeFlags(tag, givenFlags, name, isAbsType, rhsIsEmpty) def adjustIfModule(completer: LazyType) = diff --git a/compiler/src/dotty/tools/dotc/transform/Pickler.scala b/compiler/src/dotty/tools/dotc/transform/Pickler.scala index 88edae086833..2584ec146e16 100644 --- a/compiler/src/dotty/tools/dotc/transform/Pickler.scala +++ b/compiler/src/dotty/tools/dotc/transform/Pickler.scala @@ -16,7 +16,6 @@ import reporting.{ThrowingReporter, Profile, Message} import collection.mutable import util.concurrent.{Executor, Future} import compiletime.uninitialized -import dotty.tools.tasty.TastyFormat.Scala2StandardLibraryAttribute object Pickler { val name: String = "pickler" @@ -109,10 +108,10 @@ class Pickler extends Phase { pickler, treePkl.buf.addrOfTree, treePkl.docString, tree, scratch.commentBuffer) - val attributes = - if ctx.settings.YcompileScala2Library.value then List(Scala2StandardLibraryAttribute) - else Nil - AttributePickler.pickleAttributes(attributes, pickler, scratch.attributeBuffer) + AttributePickler.pickleAttributes( + ctx.settings.YcompileScala2Library.value, + pickler, + scratch.attributeBuffer) val pickled = pickler.assembleParts() diff --git a/tasty/src/dotty/tools/tasty/TastyFormat.scala b/tasty/src/dotty/tools/tasty/TastyFormat.scala index 34734d5093b8..2a29383e8651 100644 --- a/tasty/src/dotty/tools/tasty/TastyFormat.scala +++ b/tasty/src/dotty/tools/tasty/TastyFormat.scala @@ -270,7 +270,7 @@ Standard Section: "Comments" Comment* Standard Section: "Attributes" Attribute* ```none - Attribute = FLAGattr UTF8 // attributes match the regex [a-zA-Z0-9]+ + Attribute = SCALA2STANDARDLIBRARYattr ``` **************************************************************************************/ @@ -369,8 +369,6 @@ object TastyFormat { final val CommentsSection = "Comments" final val AttributesSection = "Attributes" - final val Scala2StandardLibraryAttribute = "Scala2StandardLibrary" - /** Tags used to serialize names, should update [[TastyFormat$.nameTagToString]] if a new constant is added */ class NameTags { final val UTF8 = 1 // A simple name in UTF8 encoding. @@ -609,7 +607,7 @@ object TastyFormat { // Attribute tags - final val FLAGattr = 1 + final val SCALA2STANDARDLIBRARYattr = 1 /** Useful for debugging */ def isLegalTag(tag: Int): Boolean =