summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'src/compiler/scala/tools/cmd')
-rw-r--r--src/compiler/scala/tools/cmd/gen/AnyVals.scala45
1 files changed, 28 insertions, 17 deletions
diff --git a/src/compiler/scala/tools/cmd/gen/AnyVals.scala b/src/compiler/scala/tools/cmd/gen/AnyVals.scala
index e8230b8ca4..83cd9c2578 100644
--- a/src/compiler/scala/tools/cmd/gen/AnyVals.scala
+++ b/src/compiler/scala/tools/cmd/gen/AnyVals.scala
@@ -15,18 +15,27 @@ trait AnyValReps {
case class Op(val op : String, val doc : String)
- private def companionCoercions(tos: String*) = {
+ private def companionCoercions(tos: AnyValRep*) = {
tos.toList map (to =>
- """implicit def %s2%s(x: %s): %s = x.to%s""".format(javaEquiv, to, name, to.capitalize, to.capitalize)
+ """implicit def @javaequiv@2%s(x: @name@): %s = x.to%s""".format(to.javaEquiv, to.name, to.name)
)
}
- def implicitCoercions: List[String] = javaEquiv match {
- case "byte" => companionCoercions("short", "int", "long", "float", "double")
- case "short" | "char" => companionCoercions("int", "long", "float", "double")
- case "int" => companionCoercions("long", "float", "double")
- case "long" => companionCoercions("float", "double")
- case "float" => companionCoercions("double")
- case _ => Nil
+ def coercionCommentExtra = ""
+ def coercionComment = """
+ /** Language mandated coercions from @name@ to "wider" types.%s
+ */""".format(coercionCommentExtra)
+
+ def implicitCoercions: List[String] = {
+ val coercions = this match {
+ case B => companionCoercions(S, I, L, F, D)
+ case S | C => companionCoercions(I, L, F, D)
+ case I => companionCoercions(L, F, D)
+ case L => companionCoercions(F, D)
+ case F => companionCoercions(D)
+ case _ => Nil
+ }
+ if (coercions.isEmpty) Nil
+ else coercionComment :: coercions
}
def isCardinal: Boolean = isIntegerType(this)
@@ -174,7 +183,7 @@ trait AnyValReps {
}
def objectLines = {
val comp = if (isCardinal) cardinalCompanion else floatingCompanion
- ((comp + allCompanions).trim.lines map interpolate).toList ++ implicitCoercions
+ (comp + allCompanions + "\n" + nonUnitCompanions).trim.lines.toList ++ implicitCoercions map interpolate
}
/** Makes a set of binary operations based on the given set of ops, args, and resultFn.
@@ -238,8 +247,9 @@ trait AnyValReps {
def classDoc = interpolate(classDocTemplate)
def objectDoc = ""
def mkImports = ""
- def mkClass = assemble("final class", "private", "AnyVal", classLines) + "\n"
- def mkObject = assemble("object", "", "AnyValCompanion", objectLines) + "\n"
+
+ def mkClass = assemble("final class " + name + " private extends AnyVal", classLines)
+ def mkObject = assemble("object " + name + " extends AnyValCompanion", objectLines)
def make() = List[String](
headerTemplate,
mkImports,
@@ -249,11 +259,10 @@ trait AnyValReps {
mkObject
) mkString ""
- def assemble(what: String, ctor: String, parent: String, lines: List[String]): String = {
- val decl = "%s %s %s extends %s ".format(what, name, ctor, parent)
- val body = if (lines.isEmpty) "{ }\n\n" else lines map indent mkString ("{\n", "\n", "\n}\n")
+ def assemble(decl: String, lines: List[String]): String = {
+ val body = if (lines.isEmpty) " { }\n\n" else lines map indent mkString (" {\n", "\n", "\n}\n")
- decl + body
+ decl + body + "\n"
}
override def toString = name
}
@@ -310,6 +319,8 @@ def unbox(x: java.lang.Object): @name@ = @unboxImpl@
override def toString = "object scala.@name@"
"""
+ def nonUnitCompanions = "" // todo
+
def cardinalCompanion = """
/** The smallest value representable as a @name@.
*/
@@ -446,7 +457,7 @@ def ^(x: Boolean): Boolean = sys.error("stub")
override def getClass(): Class[Boolean] = sys.error("stub")
""".trim.lines.toList
- def objectLines = interpolate(allCompanions).lines.toList
+ def objectLines = interpolate(allCompanions + "\n" + nonUnitCompanions).lines.toList
}
object U extends AnyValRep("Unit", None, "void") {
override def classDoc = """