diff options
author | Martin Odersky <odersky@gmail.com> | 2013-02-25 12:49:37 +0100 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2013-02-25 12:49:37 +0100 |
commit | 94e2c9413def4c6545cfe38ed7b8a5b44bdd6456 (patch) | |
tree | 0b26b3763804acc72a2304e3fabe63159670266c /src/dotty | |
parent | 6e37f2e4e153109125493c6c82c0ea0eb9800422 (diff) | |
download | dotty-94e2c9413def4c6545cfe38ed7b8a5b44bdd6456.tar.gz dotty-94e2c9413def4c6545cfe38ed7b8a5b44bdd6456.tar.bz2 dotty-94e2c9413def4c6545cfe38ed7b8a5b44bdd6456.zip |
Completed implementation of Flags
Including translation from Scala 2.x flags.
Diffstat (limited to 'src/dotty')
-rw-r--r-- | src/dotty/tools/dotc/core/Definitions.scala | 6 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/Flags.scala | 262 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/SymDenotations.scala | 2 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/Symbols.scala | 4 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/pickling/PickleBuffer.scala | 92 | ||||
-rw-r--r-- | src/dotty/tools/dotc/core/pickling/UnPickler.scala | 2 |
6 files changed, 242 insertions, 126 deletions
diff --git a/src/dotty/tools/dotc/core/Definitions.scala b/src/dotty/tools/dotc/core/Definitions.scala index 8b512be8a..efed9392d 100644 --- a/src/dotty/tools/dotc/core/Definitions.scala +++ b/src/dotty/tools/dotc/core/Definitions.scala @@ -26,7 +26,7 @@ class Definitions(implicit ctx: Context) { private def newSyntheticTypeParam(cls: ClassSymbol, scope: Scope, suffix: String = "T0") = { val tname = suffix.toTypeName.expandedName(cls) - val tparam = ctx.newSymbol(cls, tname, TypeParamFlags, TypeBounds.empty) + val tparam = ctx.newSymbol(cls, tname, TypeParamCreationFlags, TypeBounds.empty) scope.enter(tparam) } @@ -73,9 +73,9 @@ class Definitions(implicit ctx: Context) { lazy val NotNullClass = requiredClass("scala.NotNull") lazy val NothingClass: ClassSymbol = ctx.newCompleteClassSymbol( - ScalaPackageClass, tpnme.Nothing, UninstantiatableFlags, List(AnyClass.typeConstructor)).entered + ScalaPackageClass, tpnme.Nothing, AbstractFinal, List(AnyClass.typeConstructor)).entered lazy val NullClass: ClassSymbol = ctx.newCompleteClassSymbol( - ScalaPackageClass, tpnme.Null, UninstantiatableFlags, List(AnyRefAlias.typeConstructor)).entered + ScalaPackageClass, tpnme.Null, AbstractFinal, List(AnyRefAlias.typeConstructor)).entered lazy val PredefModule = requiredModule("scala.Predef") diff --git a/src/dotty/tools/dotc/core/Flags.scala b/src/dotty/tools/dotc/core/Flags.scala index bded3e822..31062247d 100644 --- a/src/dotty/tools/dotc/core/Flags.scala +++ b/src/dotty/tools/dotc/core/Flags.scala @@ -99,12 +99,20 @@ object Flags { private final val TYPES = 1 << TYPEindex private final val KINDFLAGS = TERMS | TYPES + private final val FirstFlag = 2 + private final val FirstNotPickledFlag = 48 private final val MaxFlag = 63 private var flagName = Array.fill(64, 2)("") private val kindIndices = Set(TERMindex, TYPEindex) + private def isDefinedAsFlag(idx: Int) = flagName(idx) exists (_.nonEmpty) + + private def flagRange(start: Int, end: Int) = + FlagSet((KINDFLAGS.toLong /: (start until end)) ((bits, idx) => + if (isDefinedAsFlag(idx)) bits | (1L << idx) else bits)) + /** The flag with given index between 2 and 63 which applies to terms. * Installs given name as the name of the flag. */ def termFlag(index: Int, name: String): FlagSet = { @@ -164,153 +172,165 @@ object Flags { final val Method = termFlag(7, "<method>") /** Labeled with `abstract` modifier (an abstract class) */ - final val Abstract = typeFlag(8, "abstract") + final val Abstract = typeFlag(7, "abstract") + + /** A (term or type) parameter to a class or method */ + final val Param = commonFlag(8, "<param>") + final val TermParam = Param.toTermFlags + final val TypeParam = Param.toTypeFlags + + /** Labeled with `implicit` modifier (implicit value) */ + final val Implicit = termFlag(9, "implicit") + + /** A trait */ + final val Trait = typeFlag(9, "<trait>") + + /** Labeled with `lazy` (a lazy val). */ + final val Lazy = termFlag(10, "lazy") /** A trait that has only abstract methods as members * (and therefore can be represented by a Java interface */ - final val Interface = typeFlag(9, "interface") + final val Interface = typeFlag(10, "interface") - /** A value or class implementing a module */ - final val Module = commonFlag(10, "module") - final val ModuleVal = Module.toTermFlags - final val ModuleClass = Module.toTypeFlags + /** A value or variable accessor (getter or setter) */ + final val Accessor = termFlag(11, "<accessor>") - /** Labeled with `implicit` modifier (implicit value) */ - final val Implicit = termFlag(11, "implicit") + /** A covariant type variable */ + final val Covariant = typeFlag(11, "<covariant>") - /** Labeled with `sealed` modifier (sealed class) */ - final val Sealed = typeFlag(12, "sealed") + /** A mutable var */ + final val Mutable = termFlag(12, "mutable") - /** A case class or its companion object */ - final val Case = commonFlag(13, "case") - final val CaseClass = Case.toTypeFlags - final val CaseVal = Case.toTermFlags + /** A contravariant type variable */ + final val Contravariant = typeFlag(12, "<contravariant>") - /** Labeled with `lazy` (a lazy val). */ - final val Lazy = termFlag(14, "lazy") + /** Symbol is local to current class (i.e. private[this] or protected[this] + * pre: Private or Protected are also set + */ + final val Local = commonFlag(13, "<local>") - /** A mutable var */ - final val Mutable = termFlag(14, "mutable") + /** A field generated for a primary constructor parameter (no matter if it's a 'val' or not), + * or an accessor of such a field. + */ + final val ParamAccessor = commonFlag(14, "<paramaccessor>") - /** A (term or type) parameter to a class or method */ - final val Param = commonFlag(15, "<param>") - final val TermParam = Param.toTermFlags - final val TypeParam = Param.toTypeFlags + /** A value or class implementing a module */ + final val Module = commonFlag(15, "module") + final val ModuleVal = Module.toTermFlags + final val ModuleClass = Module.toTypeFlags - /** A value or class representing a package */ + /** A value or class representing a package */ final val Package = commonFlag(16, "<package>") final val PackageVal = Package.toTermFlags final val PackageClass = Package.toTypeFlags /** A package object or its module class */ - final val PackageObject = commonFlag(???, "package") + final val PackageObject = commonFlag(17, "package") final val PackageObjectVal = PackageObject.toTermFlags final val PackageObjectClass = PackageObject.toTypeFlags - /** A covariant type variable */ - final val Covariant = typeFlag(17, "<covariant>") + /** A case class or its companion object */ + final val Case = commonFlag(17, "case") + final val CaseClass = Case.toTypeFlags + final val CaseVal = Case.toTermFlags - /** Method is a label. */ - final val Label = termFlag(18, "<label>") + /** A compiler-generated symbol, which is visible for type-checking + * (compare with artifact) + */ + final val Synthetic = commonFlag(18, "<synthetic>") - /** Symbol is a macro */ - final val Macro = commonFlag(???, "<macro>") + /** Symbol's name is expanded */ + final val ExpandedName = commonFlag(19, "<expandedname>") - /** A contravariant type variable */ - final val Contravariant = typeFlag(18, "<contravariant>") + /** Method is a label. */ + final val Label = termFlag(20, "<label>") + + /** Labeled with `sealed` modifier (sealed class) */ + final val Sealed = typeFlag(20, "sealed") /** Labeled with of abstract & override */ - final val AbsOverride = termFlag(19, "abstract override") + final val AbsOverride = termFlag(21, "abstract override") - /** Symbol is local to current class (i.e. private[this] or protected[this] - * pre: Private or Protected are also set - */ - final val Local = commonFlag(20, "<local>") + /** Method is assumed to be stable */ + final val Stable = termFlag(22, "<stable>") - /** Symbol is defined by a Java class */ - final val JavaDefined = commonFlag(21, "<java>") + /** A case parameter (or its accessor, or a GADT skolem) */ + final val CaseAccessor = termFlag(23, "<caseaccessor>") - /** A compiler-generated symbol. which is visible for type-checking - * (compare with artifact) - */ - final val Synthetic = commonFlag(22, "<synthetic>") + /** Class symbol is defined in this/superclass constructor. */ + final val InConstructor = typeFlag(23, "<in-constructor>") - /** Method is assumed to be stable */ - final val Stable = termFlag(23, "<stable>") + /** A super accessor */ + final val SuperAccessor = termFlag(24, "<superaccessor>") - final val Static = commonFlag(24, "<static>") + /** A parameter with a default value */ + final val DefaultParam = termFlag(25, "<defaultparam>") - /** A value or variable accessor (getter or setter) */ - final val Accessor = termFlag(25, "<accessor>") + /** Symbol is initialized to the default value, e.g. var x: T = _ */ + final val DefaultInit = termFlag(26, "<defaultinit>") - /** A case parameter (or its accessor, or a GADT skolem) */ - final val CaseAccessor = termFlag(26, "<caseaccessor>") + /** Symbol is a macro */ + final val Macro = commonFlag(27, "<macro>") - /** A super accessor */ - final val SuperAccessor = termFlag(27, "<superaccessor>") + /** Symbol is defined by a Java class */ + final val JavaDefined = commonFlag(28, "<java>") - /** A field generated for a primary constructor parameter (no matter if it's a 'val' or not), - * or an accessor of such a field. - */ - final val ParamAccessor = termFlag(28, "<paramaccessor>") + /** Symbol is implemented as a Java static */ + final val Static = commonFlag(29, "<static>") - /** A parameter with a default value */ - final val DefaultParam = termFlag(27, "<defaultparam>") + /** Variable is accessed from nested function. */ + final val Captured = termFlag(30, "<captured>") - /** A trait */ - final val Trait = typeFlag(27, "<trait>") + /** Symbol should be ignored when typechecking; will be marked ACC_SYNTHETIC in bytecode */ + final val Artifact = commonFlag(31, "<artifact>") /** A bridge method. Set by Erasure */ - final val Bridge = termFlag(28, "<bridge>") + final val Bridge = termFlag(32, "<bridge>") - /** Symbol is initialized to the default value, e.g. var x: T = _ */ - final val DefaultInit = termFlag(29, "<defaultinit>") + /** Symbol is a Java varargs bridge */ + final val VBridge = termFlag(33, "<vbridge>") - /** An error symbol */ - final val Erroneous = commonFlag(???, "<is-error>") + /** Symbol is a method which should be marked ACC_SYNCHRONIZED */ + final val Synchronized = termFlag(34, "<synchronized>") - /** Denotation is in train of being loaded and completed, flag to catch cyclic dependencies */ - final val CompletionStarted = commonFlag(???, "<locked>") + /** Symbol is a Java-style varargs method */ + final val JavaVarargs = termFlag(35, "<varargs>") - /** Variable is accessed from nested function. */ - final val Captured = termFlag(???, "<captured>") + // Flags following this one are not pickled - /** Class symbol is defined in this/superclass constructor. */ - final val Inconstructor = typeFlag(???, "<in-constructor>") + /** Denotation is in train of being loaded and completed, flag to catch cyclic dependencies */ + final val CompletionStarted = commonFlag(48, "<locked>") /** Class is not allowed to accept new members because fingerprint of subclass has been taken */ - final val Frozen = typeFlag(???, "<frozen>") + final val Frozen = typeFlag(49, "<frozen>") + + /** An error symbol */ + final val Erroneous = commonFlag(50, "<is-error>") /** Class has been lifted out to package level, local value has been lifted out to class level */ - final val Lifted = termFlag(???, "<lifted>") + final val Lifted = commonFlag(51, "<lifted>") /** Term member has been mixed in */ - final val MixedIn = termFlag(???, "<mixedin>") + final val MixedIn = commonFlag(52, "<mixedin>") /** Symbol is a generated specialized member */ - final val Specialized = commonFlag(???, "<specialized>") + final val Specialized = commonFlag(53, "<specialized>") - /** Symbol is a Java-style varargs method */ - final val JavaVarargs = termFlag(???, "<varargs>") - - /** Symbol is a Java varargs bridge */ - final val VBridge = termFlag(???, "<vbridge>") - - /** Symbol is a method which should be marked ACC_SYNCHRONIZED */ - final val Synchronized = termFlag(???, "<synchronized>") + /** Symbol is an implementation class */ + final val ImplClass = typeFlag(54, "<implclass>") - /** Symbol's name is expanded */ - final val ExpandedName = commonFlag(???, "<expandedname>") + /** An existentially bound symbol (Scala 2.x only) */ + final val Scala2Existential = typeFlag(55, "<existential>") - /** Symbol should be ignored when typechecking; will be marked ACC_SYNTHETIC in bytecode */ - final val Artifact = commonFlag(???, "<artifact>") + /** An overloaded symbol (Scala 2.x only) */ + final val Scala2Overloaded = termFlag(56, "<overloaded>") - /** Symbol is an implementation class */ - final val ImplClass = typeFlag(???, "<implclass>") + /** A module variable (Scala 2.x only) */ + final val Scala2ModuleVar = termFlag(57, "<modulevar>") - /** An existentially bound symbol (Scala 2.x only) */ - final val Scala2Existential = typeFlag(???, "<existential>") + /** A definition that's initialized before the super call (Scala 2.x only) */ + final val Scala2PreSuper = termFlag(58, "<presuper>") // --------- Combined Flag Sets and Conjunctions ---------------------- @@ -321,23 +341,12 @@ object Flags { /** Flags representing access rights */ final val AccessFlags = Private | Protected | Local - final val AbstractSealed = Abstract | Sealed - - final val SyntheticArtifact = Synthetic | Artifact - - final val RetainedModuleFlags: FlagSet = ??? - final val RetainedModuleClassFlags: FlagSet = ??? - - final val UninstantiatableFlags = Abstract | Final - - /** These flags are enabled from phase 1 */ - final val InitialFlags: FlagSet = ??? - - /** These flags are not pickled */ - final val FlagsNotPickled = commonFlags(Erroneous, Lifted, Frozen, CompletionStarted) + /** A value that's unstable unless complemented with a Stable flag */ + final val UnstableValue = Mutable | Method - /** These flags are pickled */ - final val PickledFlags = InitialFlags &~ FlagsNotPickled + /** Flags that are passed from a type parameter of a class to a refinement symbol + * that sets the type parameter */ + final val RetainedTypeArgFlags = Covariant | Contravariant | Protected | Local /** Modules always have these flags set */ final val ModuleCreationFlags = Module @@ -345,18 +354,34 @@ object Flags { /** Module classes always have these flags set */ final val ModuleClassCreationFlags = Module | Final + /** The flags of a type parameter */ + final val TypeParamCreationFlags = Protected | Local + + /** Flags that can apply to both a module val and a module class, except those that + * are added at creation anyway + */ + final val RetainedModuleValAndClassFlags: FlagSet = + AccessFlags | Package | PackageObject | Case | + Synthetic | ExpandedName | JavaDefined | Static | Artifact | + Erroneous | Lifted | MixedIn | Specialized + + /** Flags that can apply to a module val */ + final val RetainedModuleValFlags: FlagSet = RetainedModuleValAndClassFlags | + Override | Final | Method | Implicit | Lazy | + Accessor | AbsOverride | Stable | Captured | Synchronized + + /** Flags that can apply to a module class */ + final val RetainedModuleClassFlags: FlagSet = RetainedModuleValAndClassFlags | + InConstructor | ImplClass + /** Packages and package classes always have these flags set */ final val PackageCreationFlags = Module | Package | Final | JavaDefined | Static - /** A value that's unstable unless complemented with a Stable flag */ - final val UnstableValue = Mutable | Method - - /** Flags that are passed from a type parameter of a class to a refinement symbol - * that sets the type parameter */ - final val RetainedTypeArgFlags = Covariant | Contravariant | Protected | Local + /** These flags are pickled */ + final val PickledFlags = flagRange(FirstFlag, FirstNotPickledFlag) - /** An abstract class or trait */ - final val AbstractOrTrait = Trait | Abstract + /** An abstract class or a trait */ + final val AbstractOrTrait = Abstract | Trait /** Labeled `private` or `protected[local]` */ final val PrivateOrLocal = Private | Local @@ -364,9 +389,6 @@ object Flags { /** A type parameter with synthesized name */ final val ExpandedTypeParam = allOf(ExpandedName, TypeParam) - /** The flags of a type parameter */ - final val TypeParamFlags = Protected | Local - /** A Java interface */ final val JavaInterface = allOf(JavaDefined | Trait) @@ -379,6 +401,10 @@ object Flags { /** Java symbol which is `protected` and `static` */ final val StaticProtected = allOf(JavaDefined, Protected, Static) + final val AbstractFinal = allOf(Abstract, Final) + final val AbstractSealed = allOf(Abstract, Sealed) + final val SyntheticArtifact = allOf(Synthetic, Artifact) + implicit def conjToFlagSet(conj: FlagConjunction): FlagSet = FlagSet(conj.bits) }
\ No newline at end of file diff --git a/src/dotty/tools/dotc/core/SymDenotations.scala b/src/dotty/tools/dotc/core/SymDenotations.scala index f9ca96e97..a7ee385d0 100644 --- a/src/dotty/tools/dotc/core/SymDenotations.scala +++ b/src/dotty/tools/dotc/core/SymDenotations.scala @@ -881,7 +881,7 @@ object SymDenotations { class LazyModuleInfo(val mclass: ClassSymbol)(implicit cctx: CondensedContext) extends LazyType { def complete(denot: SymDenotation): Unit = { val from = denot.moduleClass.denot.asClass - denot.setFlag(from.flags.toTermFlags & RetainedModuleFlags) + denot.setFlag(from.flags.toTermFlags & RetainedModuleValFlags) denot.annotations = from.annotations filter (_.appliesToModule) denot.info = mclass.symbolicRef denot.privateWithin = from.privateWithin diff --git a/src/dotty/tools/dotc/core/Symbols.scala b/src/dotty/tools/dotc/core/Symbols.scala index 7e4cb2a69..455be5f14 100644 --- a/src/dotty/tools/dotc/core/Symbols.scala +++ b/src/dotty/tools/dotc/core/Symbols.scala @@ -122,7 +122,7 @@ trait Symbols { this: Context => infoFn(module, modcls), privateWithin) val mdenot = SymDenotation( module, owner, name, - flags & RetainedModuleFlags | ModuleCreationFlags, + flags & RetainedModuleValFlags | ModuleCreationFlags, if (cdenot.isCompleted) modcls.symbolicRef else new LazyModuleInfo(modcls)(condensed)) module.denot = mdenot @@ -133,7 +133,7 @@ trait Symbols { this: Context => /** Create a module symbol with associated module class * from its non-info fields and the fields of the module class info. * @param flags The combined flags of the module and the module class - * These are masked with RetainedModuleFlags/RetainedModuleClassFlags. + * These are masked with RetainedModuleValFlags/RetainedModuleClassFlags. */ def newCompleteModuleSymbol( owner: Symbol, diff --git a/src/dotty/tools/dotc/core/pickling/PickleBuffer.scala b/src/dotty/tools/dotc/core/pickling/PickleBuffer.scala index a007488d3..74abf0ed8 100644 --- a/src/dotty/tools/dotc/core/pickling/PickleBuffer.scala +++ b/src/dotty/tools/dotc/core/pickling/PickleBuffer.scala @@ -166,5 +166,95 @@ class PickleBuffer(data: Array[Byte], from: Int, to: Int) { def times[T](n: Int, op: ()=>T): List[T] = if (n == 0) List() else op() :: times(n-1, op) - def unpickleScalaFlags(sflags: Long, isType: Boolean): FlagSet = ??? + private final val ScalaFlagEnd = 48 + private final val ChunkBits = 8 + private final val ChunkSize = 1 << ChunkBits + private type FlagMap = Array[Array[Long]] + + private val (scalaTermFlagMap, scalaTypeFlagMap) = { + import scala.reflect.internal.Flags._ + val corr = Map( + PROTECTED -> Protected, + OVERRIDE -> Override, + PRIVATE -> Private, + ABSTRACT -> Abstract, + DEFERRED -> Deferred, + FINAL -> Final, + METHOD -> Method, + INTERFACE -> Interface, + MODULE -> Module, + IMPLICIT -> Implicit, + SEALED -> Sealed, + CASE -> Case, + MUTABLE -> Mutable, + PARAM -> Param, + PACKAGE -> Package, + MACRO -> Macro, + BYNAMEPARAM -> (Method, Covariant), + LABEL -> (Label, Contravariant), + ABSOVERRIDE -> AbsOverride, + LOCAL -> Local, + JAVA -> JavaDefined, + SYNTHETIC -> Synthetic, + STABLE -> Stable, + STATIC -> Static, + CASEACCESSOR -> CaseAccessor, + DEFAULTPARAM -> (DefaultParam, Trait), + BRIDGE -> Bridge, + ACCESSOR -> Accessor, + SUPERACCESSOR -> SuperAccessor, + PARAMACCESSOR -> ParamAccessor, + MODULEVAR -> Scala2ModuleVar, + LAZY -> Lazy, + MIXEDIN -> (MixedIn, Scala2Existential), + EXPANDEDNAME -> ExpandedName, + IMPLCLASS -> (Scala2PreSuper, ImplClass), + SPECIALIZED -> Specialized, + DEFAULTINIT -> DefaultInit, + VBRIDGE -> VBridge, + VARARGS -> JavaVarargs) + + // generate initial maps from Scala flags to Dotty flags + val termMap, typeMap = new Array[Long](64) + for (idx <- 0 until ScalaFlagEnd) + corr get (1L << idx) match { + case Some((termFlag: FlagSet, typeFlag: FlagSet)) => + termMap(idx) |= termFlag.bits + typeMap(idx) |= typeFlag.bits + case Some(commonFlag: FlagSet) => + termMap(idx) |= commonFlag.bits + typeMap(idx) |= commonFlag.bits + case _ => + } + + // Convert map so that it maps chunks of ChunkBits size at once + // instead of single bits. + def chunkMap(xs: Array[Long]): FlagMap = { + val chunked = Array.ofDim[Long]( + (xs.length + ChunkBits - 1) / ChunkBits, ChunkSize) + for (i <- 0 until chunked.length) + for (j <- 0 until ChunkSize) + for (k <- 0 until ChunkBits) + if ((j & (1 << k)) != 0) + chunked(i)(j) |= xs(i * ChunkBits + k) + chunked + } + + (chunkMap(termMap), chunkMap(typeMap)) + } + + def unpickleScalaFlags(sflags: Long, isType: Boolean): FlagSet = { + val map: FlagMap = if (isType) scalaTypeFlagMap else scalaTermFlagMap + val shift = ChunkBits + val mask = ChunkSize - 1 + assert(6 * ChunkBits == ScalaFlagEnd) + FlagSet( + map(0)((sflags >>> (shift * 0)).toInt & mask) | + map(1)((sflags >>> (shift * 1)).toInt & mask) | + map(2)((sflags >>> (shift * 2)).toInt & mask) | + map(3)((sflags >>> (shift * 3)).toInt & mask) | + map(4)((sflags >>> (shift * 4)).toInt & mask) | + map(5)((sflags >>> (shift * 5)).toInt & mask) + ) + } } diff --git a/src/dotty/tools/dotc/core/pickling/UnPickler.scala b/src/dotty/tools/dotc/core/pickling/UnPickler.scala index c8001f05d..7460c33fe 100644 --- a/src/dotty/tools/dotc/core/pickling/UnPickler.scala +++ b/src/dotty/tools/dotc/core/pickling/UnPickler.scala @@ -366,7 +366,7 @@ class UnPickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleRoot: Clas var flags1 = flags if (flags is TypeParam) { name1 = name1.expandedName(owner) - flags1 |= TypeParamFlags + flags1 |= TypeParamCreationFlags } cctx.newSymbol(owner, name1, flags1, localUnpickler, coord = start) case CLASSsym => |