Skip to content

Commit

Permalink
Use tags directly to store attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Oct 26, 2023
1 parent 51095d0 commit e8a39f0
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 38 deletions.
19 changes: 8 additions & 11 deletions compiler/src/dotty/tools/dotc/core/tasty/AttributePickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 8 additions & 12 deletions compiler/src/dotty/tools/dotc/core/tasty/AttributeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/core/tasty/TastyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/core/tasty/TreeUnpickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) =
Expand Down
9 changes: 4 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/Pickler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down
6 changes: 2 additions & 4 deletions tasty/src/dotty/tools/tasty/TastyFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
**************************************************************************************/
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -609,7 +607,7 @@ object TastyFormat {

// Attribute tags

final val FLAGattr = 1
final val SCALA2STANDARDLIBRARYattr = 1

/** Useful for debugging */
def isLegalTag(tag: Int): Boolean =
Expand Down

0 comments on commit e8a39f0

Please sign in to comment.