From b5a01906f6ad15745710e461af0676dc0506814f Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Fri, 6 Apr 2012 10:44:39 -0700 Subject: Moving flags around for performance. Also trimmed some cruft which had accrued in recent work. --- src/compiler/scala/reflect/internal/Flags.scala | 75 ++++++-------- src/compiler/scala/reflect/internal/HasFlags.scala | 72 ++++++++++++-- .../scala/reflect/internal/SymbolFlags.scala | 110 +++++---------------- src/compiler/scala/reflect/internal/Symbols.scala | 78 ++++++++++++--- src/compiler/scala/reflect/internal/Trees.scala | 9 +- .../reflect/runtime/SynchronizedSymbols.scala | 2 +- src/compiler/scala/tools/nsc/ast/DocComments.scala | 7 +- .../scala/tools/nsc/transform/AddInterfaces.scala | 5 +- src/compiler/scala/tools/nsc/transform/Mixin.scala | 9 +- .../tools/nsc/typechecker/MethodSynthesis.scala | 2 +- .../scala/tools/nsc/typechecker/Namers.scala | 2 +- 11 files changed, 198 insertions(+), 173 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/scala/reflect/internal/Flags.scala b/src/compiler/scala/reflect/internal/Flags.scala index 38f9ec6669..ce459bdd06 100644 --- a/src/compiler/scala/reflect/internal/Flags.scala +++ b/src/compiler/scala/reflect/internal/Flags.scala @@ -121,9 +121,9 @@ class ModifierFlags { // Overridden. def flagToString(flag: Long): String = "" - final val PrivateLocal: Long = PRIVATE | LOCAL - final val ProtectedLocal: Long = PROTECTED | LOCAL - final val AccessFlags: Long = PRIVATE | PROTECTED | LOCAL + final val PrivateLocal = PRIVATE | LOCAL + final val ProtectedLocal = PROTECTED | LOCAL + final val AccessFlags = PRIVATE | PROTECTED | LOCAL } object ModifierFlags extends ModifierFlags @@ -216,22 +216,22 @@ class Flags extends ModifierFlags { /** These flags can be set when class or module symbol is first created. * They are the only flags to survive a call to resetFlags(). */ - final val TopLevelCreationFlags: Long = + final val TopLevelCreationFlags = MODULE | PACKAGE | FINAL | JAVA // TODO - there's no call to slap four flags onto every package. - final val PackageFlags: Long = TopLevelCreationFlags + final val PackageFlags = TopLevelCreationFlags // FINAL not included here due to possibility of object overriding. // In fact, FINAL should not be attached regardless. We should be able // to reconstruct whether an object was marked final in source. - final val ModuleFlags: Long = MODULE + final val ModuleFlags = MODULE /** These modifiers can be set explicitly in source programs. This is * used only as the basis for the default flag mask (which ones to display * when printing a normal message.) */ - final val ExplicitFlags: Long = + final val ExplicitFlags = PRIVATE | PROTECTED | ABSTRACT | FINAL | SEALED | OVERRIDE | CASE | IMPLICIT | ABSOVERRIDE | LAZY @@ -240,7 +240,7 @@ class Flags extends ModifierFlags { final val BridgeAndPrivateFlags = BridgeFlags | PRIVATE /** These modifiers appear in TreePrinter output. */ - final val PrintableFlags: Long = + final val PrintableFlags = ExplicitFlags | BridgeFlags | LOCAL | SYNTHETIC | STABLE | CASEACCESSOR | MACRO | ACCESSOR | SUPERACCESSOR | PARAMACCESSOR | STATIC | SPECIALIZED | SYNCHRONIZED @@ -248,7 +248,7 @@ class Flags extends ModifierFlags { * from Modifiers. Others which may be applied at creation time are: * PRIVATE, LOCAL. */ - final val FieldFlags: Long = + final val FieldFlags = MUTABLE | CASEACCESSOR | PARAMACCESSOR | STATIC | FINAL | PRESUPER | LAZY /** Masks for getters and setters, where the flags are derived from those @@ -262,24 +262,23 @@ class Flags extends ModifierFlags { * flags from the method with the default. Other flags applied at creation * time are SYNTHETIC, DEFAULTPARAM, and possibly OVERRIDE. */ - final val DefaultGetterFlags: Long = - PRIVATE | PROTECTED | FINAL + final val DefaultGetterFlags = PRIVATE | PROTECTED | FINAL /** When a symbol for a method parameter is created, only these flags survive * from Modifiers. Others which may be applied at creation time are: * SYNTHETIC. */ - final val ValueParameterFlags: Long = BYNAMEPARAM | IMPLICIT | DEFAULTPARAM - final val BeanPropertyFlags = DEFERRED | OVERRIDE | STATIC - final val VarianceFlags = COVARIANT | CONTRAVARIANT + final val ValueParameterFlags = BYNAMEPARAM | IMPLICIT | DEFAULTPARAM + final val BeanPropertyFlags = DEFERRED | OVERRIDE | STATIC + final val VarianceFlags = COVARIANT | CONTRAVARIANT /** These appear to be flags which should be transferred from owner symbol * to a newly created constructor symbol. */ - final val ConstrFlags: Long = JAVA + final val ConstrFlags = JAVA /** Module flags inherited by their module-class */ - final val ModuleToClassFlags: Long = AccessFlags | TopLevelCreationFlags | CASE | SYNTHETIC + final val ModuleToClassFlags = AccessFlags | TopLevelCreationFlags | CASE | SYNTHETIC def getterFlags(fieldFlags: Long): Long = ACCESSOR + ( if ((fieldFlags & MUTABLE) != 0) fieldFlags & ~MUTABLE & ~PRESUPER @@ -309,7 +308,7 @@ class Flags extends ModifierFlags { private final val PKL_MASK = 0x00000FFF - final val PickledFlags: Long = 0xFFFFFFFFL + final val PickledFlags = 0xFFFFFFFFL private def rawPickledCorrespondence = Array( (IMPLICIT, IMPLICIT_PKL), @@ -421,8 +420,8 @@ class Flags extends ModifierFlags { case 0x8000000000000000L => "" // (1L << 63) case _ => "" } - - def accessString(flags: Long, privateWithin: String)= ( + + private def accessString(flags: Long, privateWithin: String)= ( if (privateWithin == "") { if ((flags & PrivateLocal) == PrivateLocal) "private[this]" else if ((flags & ProtectedLocal) == ProtectedLocal) "protected[this]" @@ -433,37 +432,16 @@ class Flags extends ModifierFlags { else if ((flags & PROTECTED) != 0) "protected[" + privateWithin + "]" else "private[" + privateWithin + "]" ) - def nonAccessString(flags0: Long)(flagStringFn: Long => String): String = { - val flags = flags0 & ~AccessFlags - // Fast path for common case - if (flags == 0L) "" else { - var sb: StringBuilder = null - var i = 0 - while (i <= MaxBitPosition) { - val mask = rawFlagPickledOrder(i) - if ((flags & mask) != 0L) { - val s = flagStringFn(mask) - if (s.length > 0) { - if (sb eq null) sb = new StringBuilder append s - else if (sb.length == 0) sb append s - else sb append " " append s - } - } - i += 1 - } - if (sb eq null) "" else sb.toString - } - } - + + @deprecated("Use flagString on the flag-carrying member", "2.10.0") def flagsToString(flags: Long, privateWithin: String): String = { - val access = accessString(flags, privateWithin) + val access = accessString(flags, privateWithin) val nonAccess = flagsToString(flags & ~AccessFlags) + List(nonAccess, access) filterNot (_ == "") mkString " " } - // List of the raw flags, in pickled order - protected final val MaxBitPosition = 62 - + @deprecated("Use flagString on the flag-carrying member", "2.10.0") def flagsToString(flags: Long): String = { // Fast path for common case if (flags == 0L) "" else { @@ -491,13 +469,16 @@ class Flags extends ModifierFlags { def pickledToRawFlags(pflags: Long): Long = (pflags & ~PKL_MASK) | p2r(pflags.toInt & PKL_MASK) - protected final val pickledListOrder: List[Long] = { + // List of the raw flags, in pickled order + final val MaxBitPosition = 62 + + final val pickledListOrder: List[Long] = { val all = 0 to MaxBitPosition map (1L << _) val front = rawFlags map (_.toLong) front.toList ++ (all filterNot (front contains _)) } - protected final val rawFlagPickledOrder: Array[Long] = pickledListOrder.toArray + final val rawFlagPickledOrder: Array[Long] = pickledListOrder.toArray def flagOfModifier(mod: Modifier): Long = mod match { case Modifier.`protected` => PROTECTED diff --git a/src/compiler/scala/reflect/internal/HasFlags.scala b/src/compiler/scala/reflect/internal/HasFlags.scala index 2f574b6bfa..348f81c51d 100644 --- a/src/compiler/scala/reflect/internal/HasFlags.scala +++ b/src/compiler/scala/reflect/internal/HasFlags.scala @@ -7,7 +7,6 @@ import Flags._ * with Trees) and Symbol. */ trait HasFlags { - type FlagsType type AccessBoundaryType type AnnotationType @@ -16,12 +15,7 @@ trait HasFlags { * flag methods through accessors and disallow raw flag manipulation. * And after that, perhaps, on some magical day: a typesafe enumeration. */ - protected def flags: FlagsType - - /** The printable representation of this entity's flags and access boundary, - * restricted to flags in the given mask. - */ - def hasFlagsToString(mask: FlagsType): String + protected def flags: Long /** Access level encoding: there are three scala flags (PRIVATE, PROTECTED, * and LOCAL) which combine with value privateWithin (the "foo" in private[foo]) @@ -67,6 +61,23 @@ trait HasFlags { */ def hasNoFlags(mask: Long): Boolean = !hasFlag(mask) + /** The printable representation of this entity's flags and access boundary, + * restricted to flags in the given mask. + */ + def flagString: String = flagString(flagMask) + def flagString(mask: Long): String = calculateFlagString(flags & mask) + + /** The default mask determining which flags to display. + */ + def flagMask: Long = AllFlags + + /** The string representation of a single bit, seen from this + * flag carrying entity. + */ + def resolveOverloadedFlag(flag: Long): String = Flags.flagToString(flag) + + def privateWithinString = if (hasAccessBoundary) privateWithin.toString else "" + protected def isSetting(f: Long, mask: Long) = !hasFlag(f) && ((mask & f) != 0L) protected def isClearing(f: Long, mask: Long) = hasFlag(f) && ((mask & f) != 0L) @@ -129,6 +140,49 @@ trait HasFlags { // Name def isJavaDefined = hasFlag(JAVA) + def flagBitsToString(bits: Long): String = { + // Fast path for common case + if (bits == 0L) "" else { + var sb: StringBuilder = null + var i = 0 + while (i <= MaxBitPosition) { + val flag = Flags.rawFlagPickledOrder(i) + if ((bits & flag) != 0L) { + val s = resolveOverloadedFlag(flag) + if (s.length > 0) { + if (sb eq null) sb = new StringBuilder append s + else if (sb.length == 0) sb append s + else sb append " " append s + } + } + i += 1 + } + if (sb eq null) "" else sb.toString + } + } + + def accessString: String = { + val pw = privateWithinString + + if (pw == "") { + if (hasAllFlags(PrivateLocal)) "private[this]" + else if (hasAllFlags(ProtectedLocal)) "protected[this]" + else if (hasFlag(PRIVATE)) "private" + else if (hasFlag(PROTECTED)) "protected" + else "" + } + else if (hasFlag(PROTECTED)) "protected[" + pw + "]" + else "private[" + pw + "]" + } + protected def calculateFlagString(basis: Long): String = { + val access = accessString + val nonAccess = flagBitsToString(basis & ~AccessFlags) + + if (access == "") nonAccess + else if (nonAccess == "") access + else nonAccess + " " + access + } + // Backward compat section @deprecated( "Use isTrait", "2.10.0") def hasTraitFlag = hasFlag(TRAIT) @@ -138,4 +192,8 @@ trait HasFlags { def isAbstract = hasFlag(ABSTRACT) @deprecated("Use isValueParameter or isTypeParameter", "2.10.0") def isParameter = hasFlag(PARAM) + @deprecated("Use flagString", "2.10.0") + def defaultFlagString = flagString + @deprecated("Use flagString(mask)", "2.10.0") + def hasFlagsToString(mask: Long): String = flagString(mask) } diff --git a/src/compiler/scala/reflect/internal/SymbolFlags.scala b/src/compiler/scala/reflect/internal/SymbolFlags.scala index 0b49ee2505..0c620f8346 100644 --- a/src/compiler/scala/reflect/internal/SymbolFlags.scala +++ b/src/compiler/scala/reflect/internal/SymbolFlags.scala @@ -20,9 +20,7 @@ trait SymbolFlags { /** Not mixed in under normal conditions; a powerful debugging aid. */ - trait FlagVerifier extends SymbolFlagLogic { - this: Symbol => - + trait FlagVerifier extends Symbol { private def assert0(cond: Boolean, message: => Any) { if (!cond) { Console.err.println("[flag verification failure]\n%s\n%s\n".format(atPhaseStackMessage, message)) @@ -71,29 +69,29 @@ trait SymbolFlags { assert0(!hasRaw, symbolCreationString + "\n never=%s, what=%s".format(flagsToString(neverHasFlags), what)) } } - override def initFlags(mask: Long): this.type = { + abstract override def initFlags(mask: Long): this.type = { super.initFlags(mask) verifyFlags("initFlags(" + flagsToString(mask) + ")") this } - override def setFlag(mask: Long): this.type = { + abstract override def setFlag(mask: Long): this.type = { verifyChange(true, mask, rawflags) super.setFlag(mask) verifyFlags("setFlag(" + flagsToString(mask) + ")") this } - override def resetFlag(mask: Long): this.type = { + abstract override def resetFlag(mask: Long): this.type = { verifyChange(false, mask, rawflags) super.resetFlag(mask) verifyFlags("resetFlag(" + flagsToString(mask) + ")") this } - override def flags_=(fs: Long) { + abstract override def flags_=(fs: Long) { if ((fs & ~rawflags) != 0) verifyChange(true, fs & ~rawflags, rawflags) if ((rawflags & ~fs) != 0) verifyChange(false, rawflags & ~fs, rawflags) - + super.flags_=(fs) verifyFlags("flags_=(" + flagsToString(fs) + ")") } @@ -113,58 +111,25 @@ trait SymbolFlags { trait SymbolFlagLogic { this: Symbol => - protected def alwaysHasFlags: Long = 0L - protected def neverHasFlags: Long = METHOD | MODULE - - def flagStringFn(flag: Long): String = { - val res = flag match { - // "" - case BYNAMEPARAM => - if (this.isValueParameter) "" - else if (this.isTypeParameter) "" - else "" - // "" - case CONTRAVARIANT => - if (this.isLabel) "