summaryrefslogtreecommitdiff
path: root/src/library
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-12-02 17:28:58 +0000
committerPaul Phillips <paulp@improving.org>2010-12-02 17:28:58 +0000
commit765f9aa2bfd16fc066fcfe6f068bc9ab54b863c2 (patch)
tree1b8d31bab45162415d6cc6372c7336fc62bbad2b /src/library
parenta69c1afd4ba602bd4ccc9f9aced9bfc0f6f3c5e7 (diff)
downloadscala-765f9aa2bfd16fc066fcfe6f068bc9ab54b863c2.tar.gz
scala-765f9aa2bfd16fc066fcfe6f068bc9ab54b863c2.tar.bz2
scala-765f9aa2bfd16fc066fcfe6f068bc9ab54b863c2.zip
It's a big one!
TermName and TypeName are exposed throughout the compiler based on what kind of name a given abstraction ought to have. (There remain places where one needs to create a name without knowing yet what it will be, and those will always be Names.) The nme object in the compiler now holds only term names. To reference a known type name, use tpnme: nme.List == ("List": TermName) tpnme.List == ("List": TypeName) The contents of nme and tpname are defined in traits, many of which are shared, so if a name should exist only as a Type and not a Term, it should be defined in CompilerTypeNames, but otherwise in CompilerTermNames or CompilerCommonNames. This is partially complete but I'm sure there are still many shared which should pick a side. Usage of .toTermName and .toTypeName is strongly discouraged. After the dust has settled, there will be very few places where it will make sense to hop between namespaces like that. There are some implicits to smooth everything out, most of which should be removable eventually. // these two are in no hurry to go anywhere String => TermName String => TypeName // but not String => Name: def view in the compiler is no longer implicit // these two are temporary, and can log when they kick off to help us flush // out remaining issues of "name migration" Name => TermName Name => TypeName There is more work to be done before we're properly protected from naming errors, but I will not allow another eight hour tragedy to befall lukas or anyone else! Review by rytz. (Formality.)
Diffstat (limited to 'src/library')
-rwxr-xr-xsrc/library/scala/reflect/generic/Names.scala45
-rwxr-xr-xsrc/library/scala/reflect/generic/StdNames.scala51
-rwxr-xr-xsrc/library/scala/reflect/generic/Symbols.scala18
-rwxr-xr-xsrc/library/scala/reflect/generic/Trees.scala32
-rwxr-xr-xsrc/library/scala/reflect/generic/UnPickler.scala2
5 files changed, 90 insertions, 58 deletions
diff --git a/src/library/scala/reflect/generic/Names.scala b/src/library/scala/reflect/generic/Names.scala
index 1b31726e3a..8ec05684d1 100755
--- a/src/library/scala/reflect/generic/Names.scala
+++ b/src/library/scala/reflect/generic/Names.scala
@@ -2,20 +2,39 @@ package scala.reflect
package generic
trait Names {
-
type Name >: Null <: AnyRef
-
- def newTermName(cs: Array[Char], offset: Int, len: Int): Name
- def newTermName(cs: Array[Byte], offset: Int, len: Int): Name
- def newTermName(s: String): Name
-
- def mkTermName(name: Name): Name
-
- def newTypeName(cs: Array[Char], offset: Int, len: Int): Name
- def newTypeName(cs: Array[Byte], offset: Int, len: Int): Name
- def newTypeName(s: String): Name
-
- def mkTypeName(name: Name): Name
+ type TypeName <: Name
+ type TermName <: Name
+
+ def newTermName(cs: Array[Char], offset: Int, len: Int): TermName
+ def newTermName(cs: Array[Byte], offset: Int, len: Int): TermName
+ def newTermName(s: String): TermName
+ def mkTermName(name: Name): TermName
+
+ def newTypeName(cs: Array[Char], offset: Int, len: Int): TypeName
+ def newTypeName(cs: Array[Byte], offset: Int, len: Int): TypeName
+ def newTypeName(s: String): TypeName
+ def mkTypeName(name: Name): TypeName
+
+ def isTermName(name: Name): Boolean
+ def isTypeName(name: Name): Boolean
+
+ /** These should come out before 2.9 ships, but they have to be
+ * in the library to reach a few bits like the unpickler.
+ */
+ def onNameTranslate(name: Name): Unit = ()
+ implicit def promoteTypeNamesAsNecessary(name: Name): TypeName = {
+ if (isTermName(name))
+ onNameTranslate(name)
+
+ mkTypeName(name)
+ }
+ implicit def promoteTermNamesAsNecessary(name: Name): TermName = {
+ if (isTypeName(name))
+ onNameTranslate(name)
+
+ mkTermName(name)
+ }
}
diff --git a/src/library/scala/reflect/generic/StdNames.scala b/src/library/scala/reflect/generic/StdNames.scala
index 7d2e0fde6a..f0fb790a5e 100755
--- a/src/library/scala/reflect/generic/StdNames.scala
+++ b/src/library/scala/reflect/generic/StdNames.scala
@@ -3,33 +3,48 @@ package generic
import scala.reflect.NameTransformer
-trait StdNames { self: Universe =>
+trait LowPriorityStdNames {
+ self: Universe =>
- val nme: StandardNames
+ implicit def stringToTypeName(s: String): TypeName = newTypeName(s)
+}
- def encode(str: String): Name = newTermName(NameTransformer.encode(str))
+trait StdNames extends LowPriorityStdNames {
+ self: Universe =>
- class StandardNames {
- val EXPAND_SEPARATOR_STRING = "$$"
- val LOCAL_SUFFIX_STRING = " "
+ val nme: LibraryTermNames
+ val tpnme: LibraryTypeNames
+
+ def encode(str: String): TermName = newTermName(NameTransformer.encode(str))
+
+ implicit def stringToTermName(s: String): TermName = newTermName(s)
- val EMPTY = newTermName("")
- val EMPTY_PACKAGE_NAME = newTermName("<empty>")
- val IMPORT = newTermName("<import>")
- val REFINE_CLASS_NAME = newTermName("<refinement>")
- val ROOT = newTermName("<root>")
+ trait LibraryCommonNames {
+ type NameType <: Name
+ implicit def createNameType(name: String): NameType
- val ANON_CLASS_NAME = newTermName("$anon")
- val ANON_FUN_NAME = newTermName("$anonfun")
- val MODULE_SUFFIX = newTermName("$module")
- val ROOTPKG = newTermName("_root_")
+ val EMPTY: NameType = ""
+ val ANON_FUN_NAME: NameType = "$anonfun"
+ val EMPTY_PACKAGE_NAME: NameType = "<empty>"
+ val IMPORT: NameType = "<import>"
+ val MODULE_SUFFIX: NameType = "$module"
+ val ROOT: NameType = "<root>"
+ val ROOTPKG: NameType = "_root_"
+ }
+
+ trait LibraryTermNames extends LibraryCommonNames {
+ val EXPAND_SEPARATOR_STRING = "$$"
+ val LOCAL_SUFFIX_STRING = " "
/** The expanded name of `name' relative to this class `base` with given `separator`
*/
- def expandedName(name: Name, base: Symbol, separator: String = EXPAND_SEPARATOR_STRING): Name =
+ def expandedName(name: TermName, base: Symbol, separator: String = EXPAND_SEPARATOR_STRING): TermName =
newTermName(base.fullName('$') + separator + name)
- def moduleVarName(name: Name): Name =
- newTermName(name.toString + MODULE_SUFFIX)
+ def moduleVarName(name: TermName): TermName = newTermName("" + name + MODULE_SUFFIX)
+ }
+ trait LibraryTypeNames extends LibraryCommonNames {
+ val REFINE_CLASS_NAME: NameType = "<refinement>"
+ val ANON_CLASS_NAME: NameType = "$anon"
}
}
diff --git a/src/library/scala/reflect/generic/Symbols.scala b/src/library/scala/reflect/generic/Symbols.scala
index 4dcf80efe3..1e6112eebb 100755
--- a/src/library/scala/reflect/generic/Symbols.scala
+++ b/src/library/scala/reflect/generic/Symbols.scala
@@ -142,13 +142,13 @@ trait Symbols { self: Universe =>
final def isModule = isTerm && hasFlag(MODULE)
final def isModuleClass = isClass && hasFlag(MODULE)
final def isOverloaded = hasFlag(OVERLOADED)
- final def isRefinementClass = isClass && name == mkTypeName(nme.REFINE_CLASS_NAME)
+ final def isRefinementClass = isClass && name == tpnme.REFINE_CLASS_NAME
final def isSourceMethod = isMethod && !hasFlag(STABLE) // exclude all accessors!!!
final def isTypeParameter = isType && isParameter && !isSkolem
/** Package tests */
final def isEmptyPackage = isPackage && name == nme.EMPTY_PACKAGE_NAME
- final def isEmptyPackageClass = isPackageClass && name == mkTypeName(nme.EMPTY_PACKAGE_NAME)
+ final def isEmptyPackageClass = isPackageClass && name == tpnme.EMPTY_PACKAGE_NAME
final def isPackage = isModule && hasFlag(PACKAGE)
final def isPackageClass = isClass && hasFlag(PACKAGE)
final def isRoot = isPackageClass && owner == NoSymbol
@@ -160,13 +160,13 @@ trait Symbols { self: Universe =>
// creators
- def newAbstractType(name: Name, pos: Position = NoPosition): Symbol
- def newAliasType(name: Name, pos: Position = NoPosition): Symbol
- def newClass(name: Name, pos: Position = NoPosition): Symbol
- def newMethod(name: Name, pos: Position = NoPosition): Symbol
- def newModule(name: Name, clazz: Symbol, pos: Position = NoPosition): Symbol
- def newModuleClass(name: Name, pos: Position = NoPosition): Symbol
- def newValue(name: Name, pos: Position = NoPosition): Symbol
+ def newAbstractType(name: TypeName, pos: Position = NoPosition): Symbol
+ def newAliasType(name: TypeName, pos: Position = NoPosition): Symbol
+ def newClass(name: TypeName, pos: Position = NoPosition): Symbol
+ def newMethod(name: TermName, pos: Position = NoPosition): Symbol
+ def newModule(name: TermName, clazz: Symbol, pos: Position = NoPosition): Symbol
+ def newModuleClass(name: TypeName, pos: Position = NoPosition): Symbol
+ def newValue(name: TermName, pos: Position = NoPosition): Symbol
// access to related symbols
diff --git a/src/library/scala/reflect/generic/Trees.scala b/src/library/scala/reflect/generic/Trees.scala
index 7ab4cf882b..2573b6b191 100755
--- a/src/library/scala/reflect/generic/Trees.scala
+++ b/src/library/scala/reflect/generic/Trees.scala
@@ -18,7 +18,7 @@ trait Trees { self: Universe =>
protected def flagsIntoString(flags: Long, privateWithin: String): String
/** @param privateWithin the qualifier for a private (a type name)
- * or nme.EMPTY.toTypeName, if none is given.
+ * or tpnme.EMPTY, if none is given.
* @param annotations the annotations for the definition.
* <strong>Note:</strong> the typechecker drops these annotations,
* use the AnnotationInfo's (Symbol.annotations) in later phases.
@@ -29,9 +29,7 @@ trait Trees { self: Universe =>
type AccessBoundaryType = Name
type AnnotationType = Tree
- private val emptyTypeName = mkTypeName(nme.EMPTY)
-
- def hasAccessBoundary = privateWithin != emptyTypeName
+ def hasAccessBoundary = privateWithin != tpnme.EMPTY
def hasAllFlags(mask: Long): Boolean = (flags & mask) == mask
def hasFlag(flag: Long) = (flag & flags) != 0L
def hasFlagsToString(mask: Long): String = flagsToString(
@@ -63,7 +61,7 @@ trait Trees { self: Universe =>
}
def Modifiers(flags: Long, privateWithin: Name): Modifiers = Modifiers(flags, privateWithin, List(), Map.empty)
- def Modifiers(flags: Long): Modifiers = Modifiers(flags, mkTypeName(nme.EMPTY))
+ def Modifiers(flags: Long): Modifiers = Modifiers(flags, tpnme.EMPTY)
lazy val NoMods = Modifiers(0)
@@ -204,30 +202,31 @@ trait Trees { self: Universe =>
}
/** Class definition */
- case class ClassDef(mods: Modifiers, name: Name, tparams: List[TypeDef], impl: Template)
+ case class ClassDef(mods: Modifiers, name: TypeName, tparams: List[TypeDef], impl: Template)
extends ImplDef
/** Singleton object definition
*/
- case class ModuleDef(mods: Modifiers, name: Name, impl: Template)
+ case class ModuleDef(mods: Modifiers, name: TermName, impl: Template)
extends ImplDef
abstract class ValOrDefDef extends MemberDef {
+ def name: TermName
def tpt: Tree
def rhs: Tree
}
/** Value definition
*/
- case class ValDef(mods: Modifiers, name: Name, tpt: Tree, rhs: Tree) extends ValOrDefDef
+ case class ValDef(mods: Modifiers, name: TermName, tpt: Tree, rhs: Tree) extends ValOrDefDef
/** Method definition
*/
- case class DefDef(mods: Modifiers, name: Name, tparams: List[TypeDef],
+ case class DefDef(mods: Modifiers, name: TermName, tparams: List[TypeDef],
vparamss: List[List[ValDef]], tpt: Tree, rhs: Tree) extends ValOrDefDef
/** Abstract type, type parameter, or type alias */
- case class TypeDef(mods: Modifiers, name: Name, tparams: List[TypeDef], rhs: Tree)
+ case class TypeDef(mods: Modifiers, name: TypeName, tparams: List[TypeDef], rhs: Tree)
extends MemberDef
/** <p>
@@ -247,7 +246,7 @@ trait Trees { self: Universe =>
* jumps within a Block.
* </p>
*/
- case class LabelDef(name: Name, params: List[Ident], rhs: Tree)
+ case class LabelDef(name: TermName, params: List[Ident], rhs: Tree)
extends DefTree with TermTree
@@ -415,13 +414,13 @@ trait Trees { self: Universe =>
// The symbol of an ApplyDynamic is the function symbol of `qual', or NoSymbol, if there is none.
/** Super reference */
- case class Super(qual: Name, mix: Name)
+ case class Super(qual: TypeName, mix: TypeName)
extends TermTree with SymTree
// The symbol of a Super is the class _from_ which the super reference is made.
// For instance in C.super(...), it would be C.
/** Self reference */
- case class This(qual: Name)
+ case class This(qual: TypeName)
extends TermTree with SymTree
// The symbol of a This is the class to which the this refers.
// For instance in C.this, it would be C.
@@ -431,8 +430,7 @@ trait Trees { self: Universe =>
extends RefTree
/** Identifier <name> */
- case class Ident(name: Name)
- extends RefTree
+ case class Ident(name: Name) extends RefTree { }
class BackQuotedIdent(name: Name) extends Ident(name)
@@ -604,7 +602,7 @@ trait Trees { self: Universe =>
extends TypTree
/** Type selection <qualifier> # <name>, eliminated by RefCheck */
- case class SelectFromTypeTree(qualifier: Tree, name: Name)
+ case class SelectFromTypeTree(qualifier: Tree, name: TypeName)
extends TypTree with RefTree
/** Intersection type <parent1> with ... with <parentN> { <decls> }, eliminated by RefCheck */
@@ -706,7 +704,7 @@ trait Trees { self: Universe =>
case ApplyDynamic(qual, args) (introduced by erasure, eliminated by cleanup)
// fun(args)
case Super(qual, mix) =>
- // qual.super[mix] if qual and/or mix is empty, ther are nme.EMPTY.toTypeName
+ // qual.super[mix] if qual and/or mix is empty, ther are tpnme.EMPTY
case This(qual) =>
// qual.this
case Select(qualifier, selector) =>
diff --git a/src/library/scala/reflect/generic/UnPickler.scala b/src/library/scala/reflect/generic/UnPickler.scala
index 8a370e44fa..ed103a3a43 100755
--- a/src/library/scala/reflect/generic/UnPickler.scala
+++ b/src/library/scala/reflect/generic/UnPickler.scala
@@ -133,7 +133,7 @@ abstract class UnPickler {
assert(tag == CLASSsym)
readNat(); // read length
- val result = readNameRef() == mkTypeName(nme.REFINE_CLASS_NAME)
+ val result = readNameRef() == tpnme.REFINE_CLASS_NAME
readIndex = savedIndex
result
}