Skip to content

Commit e8a39f0

Browse files
committed
Use tags directly to store attributes
1 parent 51095d0 commit e8a39f0

File tree

6 files changed

+28
-38
lines changed

6 files changed

+28
-38
lines changed

compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@ import java.nio.charset.StandardCharsets
1010
object AttributePickler:
1111

1212
def pickleAttributes(
13-
attributes: List[String],
13+
scala2StandardLibrary: Boolean,
1414
pickler: TastyPickler,
15-
buf: TastyBuffer): Unit =
16-
if attributes != Nil then
17-
pickler.newSection(AttributesSection, buf)
18-
for attribute <- attributes do
19-
val bytes = attribute.getBytes(StandardCharsets.UTF_8).nn
20-
val length = bytes.length
21-
assert("[a-zA-Z0-9]+".r.matches(attribute), s"Malformed attribute: $attribute\n. Attribute must match [a-zA-Z0-9]+")
22-
buf.writeNat(TastyFormat.FLAGattr)
23-
buf.writeNat(length)
24-
buf.writeBytes(bytes, length)
15+
buf: TastyBuffer
16+
): Unit =
17+
if scala2StandardLibrary then // or any other attribute is set
18+
pickler.newSection(AttributesSection, buf)
19+
// Pickle attributes
20+
if scala2StandardLibrary then buf.writeNat(TastyFormat.SCALA2STANDARDLIBRARYattr)
21+
2522
end pickleAttributes
2623

2724
end AttributePickler

compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,16 @@ import java.nio.charset.StandardCharsets
1010
class AttributeUnpickler(reader: TastyReader):
1111
import reader._
1212

13-
private[tasty] lazy val attributes: List[String] = {
14-
val attributesBuilder = List.newBuilder[String]
13+
lazy val scala2StandardLibrary = {
14+
var scala2StandardLibrary = false
1515
while (!isAtEnd) {
16-
val kind = readNat()
17-
assert(kind == TastyFormat.FLAGattr, "Malformed attribute kind")
18-
val length = readNat()
19-
val bytes = readBytes(length)
20-
val attribute = new String(bytes, StandardCharsets.UTF_8)
21-
assert("[a-zA-Z0-9]+".r.matches(attribute), s"Malformed attribute: $attribute\n. Attribute must match [a-zA-Z0-9]+")
22-
attributesBuilder += attribute
16+
readNat() match
17+
case TastyFormat.SCALA2STANDARDLIBRARYattr =>
18+
scala2StandardLibrary = true
19+
case attribute =>
20+
assert(false, "Unexpected attribute value: " + attribute)
2321
}
24-
attributesBuilder.result()
22+
scala2StandardLibrary
2523
}
2624

27-
def scala2Stdlib: Boolean = attributes.contains(TastyFormat.Scala2StandardLibraryAttribute)
28-
2925
end AttributeUnpickler

compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,10 @@ class TastyPrinter(bytes: Array[Byte]) {
230230

231231
def unpickle(reader: TastyReader, tastyName: NameTable): String = {
232232
sb.append(s" ${reader.endAddr.index - reader.currentAddr.index}")
233-
val attributes = new AttributeUnpickler(reader).attributes
233+
val attributeUnpickler = new AttributeUnpickler(reader)
234234
sb.append(s" attributes bytes:\n")
235-
for attribute <- attributes do
236-
sb.append(" ").append(attribute).append("\n")
235+
if attributeUnpickler.scala2StandardLibrary then
236+
sb.append(" SCALA2STANDARDLIBRARYattr\n")
237237
sb.result
238238
}
239239
}

compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ class TreeUnpickler(reader: TastyReader,
9999
/** Was unpickled class compiled with capture checks? */
100100
private var withCaptureChecks: Boolean = false
101101

102-
private val unpicklingScala2Lib =
103-
attributeUnpicklerOpt.exists(_.scala2Stdlib)
102+
private val unpicklingScala2Library =
103+
attributeUnpicklerOpt.exists(_.scala2StandardLibrary)
104104

105105
private def registerSym(addr: Addr, sym: Symbol) =
106106
symAtAddr(addr) = sym
@@ -614,7 +614,7 @@ class TreeUnpickler(reader: TastyReader,
614614
val rhsIsEmpty = nothingButMods(end)
615615
if (!rhsIsEmpty) skipTree()
616616
val (givenFlags0, annotFns, privateWithin) = readModifiers(end)
617-
val givenFlags = if isClass && unpicklingScala2Lib then givenFlags0 | Scala2x | Scala2Tasty else givenFlags0
617+
val givenFlags = if isClass && unpicklingScala2Library then givenFlags0 | Scala2x | Scala2Tasty else givenFlags0
618618
pickling.println(i"creating symbol $name at $start with flags ${givenFlags.flagsString}, isAbsType = $isAbsType, $ttag")
619619
val flags = normalizeFlags(tag, givenFlags, name, isAbsType, rhsIsEmpty)
620620
def adjustIfModule(completer: LazyType) =

compiler/src/dotty/tools/dotc/transform/Pickler.scala

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import reporting.{ThrowingReporter, Profile, Message}
1616
import collection.mutable
1717
import util.concurrent.{Executor, Future}
1818
import compiletime.uninitialized
19-
import dotty.tools.tasty.TastyFormat.Scala2StandardLibraryAttribute
2019

2120
object Pickler {
2221
val name: String = "pickler"
@@ -109,10 +108,10 @@ class Pickler extends Phase {
109108
pickler, treePkl.buf.addrOfTree, treePkl.docString, tree,
110109
scratch.commentBuffer)
111110

112-
val attributes =
113-
if ctx.settings.YcompileScala2Library.value then List(Scala2StandardLibraryAttribute)
114-
else Nil
115-
AttributePickler.pickleAttributes(attributes, pickler, scratch.attributeBuffer)
111+
AttributePickler.pickleAttributes(
112+
ctx.settings.YcompileScala2Library.value,
113+
pickler,
114+
scratch.attributeBuffer)
116115

117116
val pickled = pickler.assembleParts()
118117

tasty/src/dotty/tools/tasty/TastyFormat.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ Standard Section: "Comments" Comment*
270270
271271
Standard Section: "Attributes" Attribute*
272272
```none
273-
Attribute = FLAGattr UTF8 // attributes match the regex [a-zA-Z0-9]+
273+
Attribute = SCALA2STANDARDLIBRARYattr
274274
```
275275
276276
**************************************************************************************/
@@ -369,8 +369,6 @@ object TastyFormat {
369369
final val CommentsSection = "Comments"
370370
final val AttributesSection = "Attributes"
371371

372-
final val Scala2StandardLibraryAttribute = "Scala2StandardLibrary"
373-
374372
/** Tags used to serialize names, should update [[TastyFormat$.nameTagToString]] if a new constant is added */
375373
class NameTags {
376374
final val UTF8 = 1 // A simple name in UTF8 encoding.
@@ -609,7 +607,7 @@ object TastyFormat {
609607

610608
// Attribute tags
611609

612-
final val FLAGattr = 1
610+
final val SCALA2STANDARDLIBRARYattr = 1
613611

614612
/** Useful for debugging */
615613
def isLegalTag(tag: Int): Boolean =

0 commit comments

Comments
 (0)