From cad4d300a9a1e68c610163745db4bcedd198d266 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Thu, 30 Nov 2023 13:52:54 +0100 Subject: [PATCH] Shrink reserved TASTy attribute categories --- .../dotc/core/tasty/AttributePickler.scala | 8 +++--- .../dotc/core/tasty/AttributeUnpickler.scala | 6 ++--- .../tools/dotc/core/tasty/TastyPrinter.scala | 4 +-- tasty/src/dotty/tools/tasty/TastyFormat.scala | 26 ++++++++++++------- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala index 6874c3fd51f2..5aea55bce4de 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala @@ -3,7 +3,7 @@ package dotty.tools.dotc.core.tasty import dotty.tools.dotc.ast.{tpd, untpd} import dotty.tools.tasty.TastyBuffer -import dotty.tools.tasty.TastyFormat, TastyFormat.AttributesSection +import dotty.tools.tasty.TastyFormat.* object AttributePickler: @@ -15,12 +15,12 @@ object AttributePickler: pickler.newSection(AttributesSection, buf) for tag <- attributes.booleanTags do - assert(tag < TastyFormat.firstStringAttrTag, "Not a boolean attribute tag: " + tag) + assert(isBooleanAttrTag(tag), "Not a boolean attribute tag: " + tag) buf.writeByte(tag) - assert(attributes.stringTagValues.exists(_._1 == TastyFormat.SOURCEFILEattr)) + assert(attributes.stringTagValues.exists(_._1 == SOURCEFILEattr)) for (tag, value) <- attributes.stringTagValues do - assert(TastyFormat.firstStringAttrTag <= tag && tag < TastyFormat.firstUnassignedAttrTag, "Not a string attribute tag: " + tag) + assert(isStringAttrTag(tag), "Not a string attribute tag: " + tag) val utf8Ref = pickler.nameBuffer.utf8Index(value) buf.writeByte(tag) buf.writeNat(utf8Ref.index) diff --git a/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala b/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala index f8779cf9703c..405f110dbca1 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala @@ -5,7 +5,7 @@ import scala.language.unsafeNulls import scala.collection.immutable.BitSet import scala.collection.immutable.TreeMap -import dotty.tools.tasty.{TastyFormat, TastyReader, TastyBuffer} +import dotty.tools.tasty.{TastyFormat, TastyReader, TastyBuffer}, TastyFormat.{isBooleanAttrTag, isStringAttrTag} import dotty.tools.dotc.core.tasty.TastyUnpickler.NameTable class AttributeUnpickler(reader: TastyReader, nameAtRef: NameTable): @@ -17,9 +17,9 @@ class AttributeUnpickler(reader: TastyReader, nameAtRef: NameTable): while !isAtEnd do val tag = readByte() - if tag < TastyFormat.firstStringAttrTag then + if isBooleanAttrTag(tag) then booleanTags += tag - else if tag < TastyFormat.firstUnassignedAttrTag then + else if isStringAttrTag(tag) then val utf8Ref = readNameRef() val value = nameAtRef(utf8Ref).toString stringTagValue += tag -> value diff --git a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala index 8ce60dd1996c..74de7ca871d4 100644 --- a/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala +++ b/compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala @@ -236,8 +236,8 @@ class TastyPrinter(bytes: Array[Byte]) { while !isAtEnd do val tag = readByte() sb.append(" ").append(attributeTagToString(tag)) - if tag < firstStringAttrTag then () - else if tag < firstUnassignedAttrTag then + if isBooleanAttrTag(tag) then () + else if isBooleanAttrTag(tag) then val utf8Ref = readNameRef() val value = nameAtRef(utf8Ref).toString sb.append(nameStr(s" ${utf8Ref.index} [$value]")) diff --git a/tasty/src/dotty/tools/tasty/TastyFormat.scala b/tasty/src/dotty/tools/tasty/TastyFormat.scala index bc4178f22c96..846f37d4013e 100644 --- a/tasty/src/dotty/tools/tasty/TastyFormat.scala +++ b/tasty/src/dotty/tools/tasty/TastyFormat.scala @@ -282,10 +282,13 @@ Standard Section: "Attributes" Attribute* SOURCEFILEattr Utf8Ref ``` -Note: Attribute tags are grouped into 2 categories that determine what follows, and thus allow to compute the size of the tagged tree in a generic way. +Note: Attribute tags are grouped into categories that determine what follows, and thus allow to compute the size of the tagged tree in a generic way. + Unassigned categories can be used to extend and existing category or to add new kinds of attributes ```none - Attribute Category 1 (tags 1-64) : tag - Attribute Category 2 (tags 65-128): tag Utf8Ref + Attribute Category 1 (tags 1-32) : tag + Attribute Category 2 (tags 33-64): // not assigned yet + Attribute Category 3 (tags 129-160): tag Utf8Ref + Attribute Category 4 (tags 161-255): // not assigned yet ``` **************************************************************************************/ @@ -617,8 +620,8 @@ object TastyFormat { // Attributes tags - // Attr Cat. 1: tag - final val firstBooleanAttrTag = SCALA2STANDARDLIBRARYattr + // Attribute Category 1 (tags 1-32) : tag + def isBooleanAttrTag(tag: Int): Boolean = 1 <= tag && tag <= 32 final val SCALA2STANDARDLIBRARYattr = 1 final val EXPLICITNULLSattr = 2 final val CAPTURECHECKEDattr = 3 @@ -626,12 +629,15 @@ object TastyFormat { final val JAVAattr = 5 final val OUTLINEattr = 6 - // Attr Cat. 2: tag UTF8 - final val firstStringAttrTag = SOURCEFILEattr - final val SOURCEFILEattr = 128 + // Attribute Category 2 (tags 33-64): unassigned - // Attr Cat. 3: unassigned - final val firstUnassignedAttrTag = 129 + // Attribute Category 3 (tags 129-160): tag Utf8Ref + def isStringAttrTag(tag: Int): Boolean = 129 <= tag && tag <= 160 + final val SOURCEFILEattr = 129 + + // Attribute Category 4 (tags 161-255): unassigned + + // end of Attributes tags /** Useful for debugging */