aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/dotty/tools/dotc/core
diff options
context:
space:
mode:
authorodersky <odersky@gmail.com>2017-02-16 10:56:46 +0100
committerGitHub <noreply@github.com>2017-02-16 10:56:46 +0100
commit6df672c7e7be65d7be1cd6524c610aed4f35178c (patch)
treea1b709dc631f0891b6a2c7c628439443c75f14e2 /compiler/src/dotty/tools/dotc/core
parent39af2f595979c6bbeb3cfa52d401cf59be68126b (diff)
parent93a2d0653e6b74a0f88825ac8a522da87e474f2a (diff)
downloaddotty-6df672c7e7be65d7be1cd6524c610aed4f35178c.tar.gz
dotty-6df672c7e7be65d7be1cd6524c610aed4f35178c.tar.bz2
dotty-6df672c7e7be65d7be1cd6524c610aed4f35178c.zip
Merge pull request #1962 from dotty-staging/centralize-function-logic
Factor out logic for scala functions.
Diffstat (limited to 'compiler/src/dotty/tools/dotc/core')
-rw-r--r--compiler/src/dotty/tools/dotc/core/Definitions.scala90
-rw-r--r--compiler/src/dotty/tools/dotc/core/NameOps.scala45
-rw-r--r--compiler/src/dotty/tools/dotc/core/TypeErasure.scala11
3 files changed, 105 insertions, 41 deletions
diff --git a/compiler/src/dotty/tools/dotc/core/Definitions.scala b/compiler/src/dotty/tools/dotc/core/Definitions.scala
index 01a164a81..9e9e39a84 100644
--- a/compiler/src/dotty/tools/dotc/core/Definitions.scala
+++ b/compiler/src/dotty/tools/dotc/core/Definitions.scala
@@ -637,13 +637,9 @@ class Definitions {
FunctionType(args.length, isImplicit).appliedTo(args ::: resultType :: Nil)
def unapply(ft: Type)(implicit ctx: Context) = {
val tsym = ft.typeSymbol
- val isImplicitFun = isImplicitFunctionClass(tsym)
- if (isImplicitFun || isFunctionClass(tsym)) {
- val targs = ft.argInfos
- val numArgs = targs.length - 1
- if (numArgs >= 0 && FunctionType(numArgs, isImplicitFun).symbol == tsym)
- Some(targs.init, targs.last, isImplicitFun)
- else None
+ if (isFunctionClass(tsym)) {
+ val targs = ft.dealias.argInfos
+ Some(targs.init, targs.last, tsym.name.isImplicitFunction)
}
else None
}
@@ -696,20 +692,17 @@ class Definitions {
lazy val TupleType = mkArityArray("scala.Tuple", MaxTupleArity, 2)
lazy val ProductNType = mkArityArray("scala.Product", MaxTupleArity, 0)
- def FunctionClass(n: Int)(implicit ctx: Context) =
- if (n < MaxImplementedFunctionArity) FunctionClassPerRun()(ctx)(n)
+ def FunctionClass(n: Int, isImplicit: Boolean = false)(implicit ctx: Context) =
+ if (isImplicit) ctx.requiredClass("scala.ImplicitFunction" + n.toString)
+ else if (n <= MaxImplementedFunctionArity) FunctionClassPerRun()(ctx)(n)
else ctx.requiredClass("scala.Function" + n.toString)
lazy val Function0_applyR = ImplementedFunctionType(0).symbol.requiredMethodRef(nme.apply)
def Function0_apply(implicit ctx: Context) = Function0_applyR.symbol
- def ImplicitFunctionClass(n: Int)(implicit ctx: Context) =
- ctx.requiredClass("scala.ImplicitFunction" + n.toString)
-
def FunctionType(n: Int, isImplicit: Boolean = false)(implicit ctx: Context): TypeRef =
- if (isImplicit && !ctx.erasedTypes) ImplicitFunctionClass(n).typeRef
- else if (n < MaxImplementedFunctionArity) ImplementedFunctionType(n)
- else FunctionClass(n).typeRef
+ if (n <= MaxImplementedFunctionArity && (!isImplicit || ctx.erasedTypes)) ImplementedFunctionType(n)
+ else FunctionClass(n, isImplicit).typeRef
private lazy val TupleTypes: Set[TypeRef] = TupleType.toSet
private lazy val ProductTypes: Set[TypeRef] = ProductNType.toSet
@@ -733,14 +726,61 @@ class Definitions {
def isBottomType(tp: Type) =
tp.derivesFrom(NothingClass) || tp.derivesFrom(NullClass)
- def isFunctionClass(cls: Symbol) = isVarArityClass(cls, tpnme.Function)
- def isImplicitFunctionClass(cls: Symbol) = isVarArityClass(cls, tpnme.ImplicitFunction)
- /** Is a class that will be erased to FunctionXXL */
- def isXXLFunctionClass(cls: Symbol) = cls.name.functionArity > MaxImplementedFunctionArity
+ /** Is a function class.
+ * - FunctionN for N >= 0
+ * - ImplicitFunctionN for N >= 0
+ */
+ def isFunctionClass(cls: Symbol) = scalaClassName(cls).isFunction
+
+ /** Is an implicit function class.
+ * - ImplicitFunctionN for N >= 0
+ */
+ def isImplicitFunctionClass(cls: Symbol) = scalaClassName(cls).isImplicitFunction
+
+ /** Is a class that will be erased to FunctionXXL
+ * - FunctionN for N >= 22
+ * - ImplicitFunctionN for N >= 22
+ */
+ def isXXLFunctionClass(cls: Symbol) = scalaClassName(cls).functionArity > MaxImplementedFunctionArity
+
+ /** Is a synthetic function class
+ * - FunctionN for N > 22
+ * - ImplicitFunctionN for N >= 0
+ */
+ def isSyntheticFunctionClass(cls: Symbol) = scalaClassName(cls).isSyntheticFunction
+
def isAbstractFunctionClass(cls: Symbol) = isVarArityClass(cls, tpnme.AbstractFunction)
def isTupleClass(cls: Symbol) = isVarArityClass(cls, tpnme.Tuple)
def isProductClass(cls: Symbol) = isVarArityClass(cls, tpnme.Product)
+ /** Returns the erased class of the function class `cls`
+ * - FunctionN for N > 22 becomes FunctionXXL
+ * - FunctionN for 22 > N >= 0 remains as FunctionN
+ * - ImplicitFunctionN for N > 22 becomes FunctionXXL
+ * - ImplicitFunctionN for 22 > N >= 0 becomes FunctionN
+ * - anything else becomes a NoSymbol
+ */
+ def erasedFunctionClass(cls: Symbol): Symbol = {
+ val arity = scalaClassName(cls).functionArity
+ if (arity > 22) defn.FunctionXXLClass
+ else if (arity >= 0) defn.FunctionClass(arity)
+ else NoSymbol
+ }
+
+ /** Returns the erased type of the function class `cls`
+ * - FunctionN for N > 22 becomes FunctionXXL
+ * - FunctionN for 22 > N >= 0 remains as FunctionN
+ * - ImplicitFunctionN for N > 22 becomes FunctionXXL
+ * - ImplicitFunctionN for 22 > N >= 0 becomes FunctionN
+ * - anything else becomes a NoType
+ */
+ def erasedFunctionType(cls: Symbol): Type = {
+ val arity = scalaClassName(cls).functionArity
+ if (arity > 22) defn.FunctionXXLType
+ else if (arity >= 0) defn.FunctionType(arity)
+ else NoType
+ }
+
val predefClassNames: Set[Name] =
Set("Predef$", "DeprecatedPredef", "LowPriorityImplicits").map(_.toTypeName)
@@ -811,16 +851,13 @@ class Definitions {
def isFunctionType(tp: Type)(implicit ctx: Context) = {
val arity = functionArity(tp)
val sym = tp.dealias.typeSymbol
- arity >= 0 && (
- isFunctionClass(sym) && tp.isRef(FunctionType(arity, isImplicit = false).typeSymbol) ||
- isImplicitFunctionClass(sym) && tp.isRef(FunctionType(arity, isImplicit = true).typeSymbol)
- )
+ arity >= 0 && isFunctionClass(sym) && tp.isRef(FunctionType(arity, sym.name.isImplicitFunction).typeSymbol)
}
def functionArity(tp: Type)(implicit ctx: Context) = tp.dealias.argInfos.length - 1
def isImplicitFunctionType(tp: Type)(implicit ctx: Context) =
- isFunctionType(tp) && tp.dealias.typeSymbol.name.startsWith(tpnme.ImplicitFunction)
+ isFunctionType(tp) && tp.dealias.typeSymbol.name.isImplicitFunction
// ----- primitive value class machinery ------------------------------------------
@@ -894,9 +931,6 @@ class Definitions {
// ----- Initialization ---------------------------------------------------
- private def maxImplemented(name: Name) =
- if (name `startsWith` tpnme.Function) MaxImplementedFunctionArity else 0
-
/** Give the scala package a scope where a FunctionN trait is automatically
* added when someone looks for it.
*/
@@ -906,7 +940,7 @@ class Definitions {
val newDecls = new MutableScope(oldDecls) {
override def lookupEntry(name: Name)(implicit ctx: Context): ScopeEntry = {
val res = super.lookupEntry(name)
- if (res == null && name.isTypeName && name.functionArity > maxImplemented(name))
+ if (res == null && name.isTypeName && name.isSyntheticFunction)
newScopeEntry(newFunctionNTrait(name.asTypeName))
else res
}
diff --git a/compiler/src/dotty/tools/dotc/core/NameOps.scala b/compiler/src/dotty/tools/dotc/core/NameOps.scala
index c037d1ce7..cd3ae2a25 100644
--- a/compiler/src/dotty/tools/dotc/core/NameOps.scala
+++ b/compiler/src/dotty/tools/dotc/core/NameOps.scala
@@ -8,6 +8,7 @@ import Names._, StdNames._, Contexts._, Symbols._, Flags._
import Decorators.StringDecorator
import util.{Chars, NameTransformer}
import Chars.isOperatorPart
+import Definitions._
object NameOps {
@@ -231,13 +232,43 @@ object NameOps {
}
}
- def functionArity: Int = {
- def test(prefix: Name): Int =
- if (name.startsWith(prefix))
- try name.drop(prefix.length).toString.toInt
- catch { case ex: NumberFormatException => -1 }
- else -1
- test(tpnme.Function) max test(tpnme.ImplicitFunction)
+ /** Is a synthetic function name
+ * - N for FunctionN
+ * - N for ImplicitFunctionN
+ * - (-1) otherwise
+ */
+ def functionArity: Int =
+ functionArityFor(tpnme.Function) max functionArityFor(tpnme.ImplicitFunction)
+
+ /** Is a function name
+ * - FunctionN for N >= 0
+ * - ImplicitFunctionN for N >= 0
+ * - false otherwise
+ */
+ def isFunction: Boolean = functionArity >= 0
+
+ /** Is a implicit function name
+ * - ImplicitFunctionN for N >= 0
+ * - false otherwise
+ */
+ def isImplicitFunction: Boolean = functionArityFor(tpnme.ImplicitFunction) >= 0
+
+ /** Is a synthetic function name
+ * - FunctionN for N > 22
+ * - ImplicitFunctionN for N >= 0
+ * - false otherwise
+ */
+ def isSyntheticFunction: Boolean = {
+ functionArityFor(tpnme.Function) > MaxImplementedFunctionArity ||
+ functionArityFor(tpnme.ImplicitFunction) >= 0
+ }
+
+ /** Parsed function arity for function with some specific prefix */
+ private def functionArityFor(prefix: Name): Int = {
+ if (name.startsWith(prefix))
+ try name.toString.substring(prefix.length).toInt
+ catch { case _: NumberFormatException => -1 }
+ else -1
}
/** The name of the generic runtime operation corresponding to an array operation */
diff --git a/compiler/src/dotty/tools/dotc/core/TypeErasure.scala b/compiler/src/dotty/tools/dotc/core/TypeErasure.scala
index 2a341390b..ff99008bb 100644
--- a/compiler/src/dotty/tools/dotc/core/TypeErasure.scala
+++ b/compiler/src/dotty/tools/dotc/core/TypeErasure.scala
@@ -44,7 +44,7 @@ object TypeErasure {
val sym = tp.symbol
sym.isClass &&
sym != defn.AnyClass && sym != defn.ArrayClass &&
- !defn.isXXLFunctionClass(sym) && !defn.isImplicitFunctionClass(sym)
+ !defn.isSyntheticFunctionClass(sym)
case _: TermRef =>
true
case JavaArrayType(elem) =>
@@ -358,8 +358,7 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
if (!sym.isClass) this(tp.info)
else if (semiEraseVCs && isDerivedValueClass(sym)) eraseDerivedValueClassRef(tp)
else if (sym == defn.ArrayClass) apply(tp.appliedTo(TypeBounds.empty)) // i966 shows that we can hit a raw Array type.
- else if (defn.isXXLFunctionClass(sym)) defn.FunctionXXLType
- else if (defn.isImplicitFunctionClass(sym)) apply(defn.FunctionType(sym.name.functionArity))
+ else if (defn.isSyntheticFunctionClass(sym)) defn.erasedFunctionType(sym)
else eraseNormalClassRef(tp)
case tp: RefinedType =>
val parent = tp.parent
@@ -370,7 +369,7 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
case SuperType(thistpe, supertpe) =>
SuperType(this(thistpe), this(supertpe))
case ExprType(rt) =>
- defn.FunctionClass(0).typeRef
+ defn.FunctionType(0)
case AndType(tp1, tp2) =>
erasedGlb(this(tp1), this(tp2), isJava)
case OrType(tp1, tp2) =>
@@ -496,8 +495,8 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
val erasedVCRef = eraseDerivedValueClassRef(tp)
if (erasedVCRef.exists) return sigName(erasedVCRef)
}
- if (defn.isImplicitFunctionClass(sym))
- sigName(defn.FunctionType(sym.name.functionArity))
+ if (defn.isSyntheticFunctionClass(sym))
+ sigName(defn.erasedFunctionType(sym))
else
normalizeClass(sym.asClass).fullName.asTypeName
case defn.ArrayOf(elem) =>