diff options
author | Paul Phillips <paulp@improving.org> | 2012-04-09 10:12:03 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-04-09 19:43:38 -0700 |
commit | c456bebbcddae490a2ab2860855e936adb60539e (patch) | |
tree | 977cded8b50c1efa7e12bc2aee55d62fa91641d7 /src/compiler/scala/reflect/internal/Definitions.scala | |
parent | 54496744806e9617d8ab745122080c48f60c64e7 (diff) | |
download | scala-c456bebbcddae490a2ab2860855e936adb60539e.tar.gz scala-c456bebbcddae490a2ab2860855e936adb60539e.tar.bz2 scala-c456bebbcddae490a2ab2860855e936adb60539e.zip |
Less laborious type application.
And eliminating redundancy. Reduced gratuitous usage of typeConstructor
on symbols which don't have type parameters.
Diffstat (limited to 'src/compiler/scala/reflect/internal/Definitions.scala')
-rw-r--r-- | src/compiler/scala/reflect/internal/Definitions.scala | 115 |
1 files changed, 47 insertions, 68 deletions
diff --git a/src/compiler/scala/reflect/internal/Definitions.scala b/src/compiler/scala/reflect/internal/Definitions.scala index 4fd98ad7b3..6ef6751720 100644 --- a/src/compiler/scala/reflect/internal/Definitions.scala +++ b/src/compiler/scala/reflect/internal/Definitions.scala @@ -198,14 +198,14 @@ trait Definitions extends reflect.api.StandardDefinitions { lazy val JavaLangEnumClass = getRequiredClass("java.lang.Enum") // convenient one-argument parameter lists - lazy val anyparam = List(AnyClass.typeConstructor) + lazy val anyparam = List(AnyClass.tpe) lazy val anyvalparam = List(AnyValClass.typeConstructor) lazy val anyrefparam = List(AnyRefClass.typeConstructor) // private parameter conveniences - private def booltype = BooleanClass.typeConstructor - private def inttype = IntClass.typeConstructor - private def stringtype = StringClass.typeConstructor + private def booltype = BooleanClass.tpe + private def inttype = IntClass.tpe + private def stringtype = StringClass.tpe // Java types def javaTypeName(jclazz: Class[_]): TypeName = newTypeName(jclazz.getName) @@ -249,7 +249,7 @@ trait Definitions extends reflect.api.StandardDefinitions { // top types lazy val AnyClass = enterNewClass(ScalaPackageClass, tpnme.Any, Nil, ABSTRACT) - lazy val AnyRefClass = newAlias(ScalaPackageClass, tpnme.AnyRef, ObjectClass.typeConstructor) + lazy val AnyRefClass = newAlias(ScalaPackageClass, tpnme.AnyRef, ObjectClass.tpe) lazy val ObjectClass = getClass(sn.Object) // Note: this is not the type alias AnyRef, it's a companion-like @@ -376,10 +376,10 @@ trait Definitions extends reflect.api.StandardDefinitions { lazy val RemoteInterfaceClass = getRequiredClass("java.rmi.Remote") lazy val RemoteExceptionClass = getRequiredClass("java.rmi.RemoteException") - lazy val ByNameParamClass = specialPolyClass(tpnme.BYNAME_PARAM_CLASS_NAME, COVARIANT)(_ => AnyClass.typeConstructor) - lazy val EqualsPatternClass = specialPolyClass(tpnme.EQUALS_PATTERN_NAME, 0L)(_ => AnyClass.typeConstructor) - lazy val JavaRepeatedParamClass = specialPolyClass(tpnme.JAVA_REPEATED_PARAM_CLASS_NAME, COVARIANT)(tparam => arrayType(tparam.typeConstructor)) - lazy val RepeatedParamClass = specialPolyClass(tpnme.REPEATED_PARAM_CLASS_NAME, COVARIANT)(tparam => seqType(tparam.typeConstructor)) + lazy val ByNameParamClass = specialPolyClass(tpnme.BYNAME_PARAM_CLASS_NAME, COVARIANT)(_ => AnyClass.tpe) + lazy val EqualsPatternClass = specialPolyClass(tpnme.EQUALS_PATTERN_NAME, 0L)(_ => AnyClass.tpe) + lazy val JavaRepeatedParamClass = specialPolyClass(tpnme.JAVA_REPEATED_PARAM_CLASS_NAME, COVARIANT)(tparam => arrayType(tparam.tpe)) + lazy val RepeatedParamClass = specialPolyClass(tpnme.REPEATED_PARAM_CLASS_NAME, COVARIANT)(tparam => seqType(tparam.tpe)) def isByNameParamType(tp: Type) = tp.typeSymbol == ByNameParamClass def isScalaRepeatedParamType(tp: Type) = tp.typeSymbol == RepeatedParamClass @@ -467,7 +467,6 @@ trait Definitions extends reflect.api.StandardDefinitions { lazy val OptionClass: Symbol = getRequiredClass("scala.Option") lazy val SomeClass: Symbol = getRequiredClass("scala.Some") lazy val NoneModule: Symbol = getRequiredModule("scala.None") - lazy val SomeModule: Symbol = getRequiredModule("scala.Some") /** Note: don't use this manifest/type function for anything important, * as it is incomplete. Would love to have things like existential types @@ -480,7 +479,7 @@ trait Definitions extends reflect.api.StandardDefinitions { val args = m.typeArguments if ((sym eq NoSymbol) || args.isEmpty) sym.tpe - else appliedType(sym.typeConstructor, args map manifestToType) + else appliedType(sym, args map manifestToType: _*) case _ => NoType } @@ -511,21 +510,21 @@ trait Definitions extends reflect.api.StandardDefinitions { def hasJavaMainMethod(path: String): Boolean = hasJavaMainMethod(getModuleIfDefined(path)) - def isOptionType(tp: Type) = cond(tp.normalize) { case TypeRef(_, OptionClass, List(_)) => true } - def isSomeType(tp: Type) = cond(tp.normalize) { case TypeRef(_, SomeClass, List(_)) => true } - def isNoneType(tp: Type) = cond(tp.normalize) { case TypeRef(_, NoneModule, List(_)) => true } + def isOptionType(tp: Type) = tp.typeSymbol isSubClass OptionClass + def isSomeType(tp: Type) = tp.typeSymbol eq SomeClass + def isNoneType(tp: Type) = tp.typeSymbol eq NoneModule - def optionType(tp: Type) = typeRef(NoPrefix, OptionClass, List(tp)) - def someType(tp: Type) = typeRef(NoPrefix, SomeClass, List(tp)) - def symbolType = typeRef(SymbolClass.typeConstructor.prefix, SymbolClass, List()) - def longType = typeRef(LongClass.typeConstructor.prefix, LongClass, List()) - - // Product, Tuple, Function + // Product, Tuple, Function, AbstractFunction private def mkArityArray(name: String, arity: Int, countFrom: Int = 1): Array[Symbol] = { val list = countFrom to arity map (i => getRequiredClass("scala." + name + i)) if (countFrom == 0) list.toArray else (NoSymbol +: list).toArray } + private def aritySpecificType(symbolArray: Array[Symbol], args: List[Type], others: Type*): Type = { + val arity = args.length + if (arity >= symbolArray.length) NoType + else appliedType(symbolArray(arity), args ++ others: _*) + } val MaxTupleArity, MaxProductArity, MaxFunctionArity = 22 /** The maximal dimensions of a generic array creation. @@ -533,11 +532,17 @@ trait Definitions extends reflect.api.StandardDefinitions { * nested array. More is not allowed. */ val MaxArrayDims = 5 - lazy val TupleClass = mkArityArray("Tuple", MaxTupleArity) - lazy val ProductClass = mkArityArray("Product", MaxProductArity) - lazy val FunctionClass = mkArityArray("Function", MaxFunctionArity, 0) + lazy val ProductClass = { val arr = mkArityArray("Product", MaxProductArity) ; arr(0) = UnitClass ; arr } + lazy val TupleClass = mkArityArray("Tuple", MaxTupleArity) + lazy val FunctionClass = mkArityArray("Function", MaxFunctionArity, 0) lazy val AbstractFunctionClass = mkArityArray("runtime.AbstractFunction", MaxFunctionArity, 0) - lazy val isProductNClass = ProductClass.toSet + + /** Creators for TupleN, ProductN, FunctionN. */ + def tupleType(elems: List[Type]) = aritySpecificType(TupleClass, elems) + def productType(elems: List[Type]) = aritySpecificType(ProductClass, elems) + def functionType(formals: List[Type], restpe: Type) = aritySpecificType(FunctionClass, formals, restpe) + def abstractFunctionType(formals: List[Type], restpe: Type) = aritySpecificType(AbstractFunctionClass, formals, restpe) + def wrapArrayMethodName(elemtp: Type): TermName = elemtp.typeSymbol match { case ByteClass => nme.wrapByteArray case ShortClass => nme.wrapShortArray @@ -554,10 +559,11 @@ trait Definitions extends reflect.api.StandardDefinitions { } @deprecated("Use isTupleType", "2.10.0") - def isTupleTypeOrSubtype(tp: Type): Boolean = isTupleType(tp) + def isTupleTypeOrSubtype(tp: Type) = isTupleType(tp) def tupleField(n: Int, j: Int) = getMember(TupleClass(n), nme.productAccessorName(j)) def isTupleSymbol(sym: Symbol) = TupleClass contains unspecializedSymbol(sym) + def isProductNClass(sym: Symbol) = ProductClass contains sym def unspecializedSymbol(sym: Symbol): Symbol = { if (sym hasFlag SPECIALIZED) { @@ -594,14 +600,6 @@ trait Definitions extends reflect.api.StandardDefinitions { } def isTupleType(tp: Type) = isTupleTypeDirect(tp.normalize) - def tupleType(elems: List[Type]) = { - val len = elems.length - if (len <= MaxTupleArity) { - val sym = TupleClass(len) - typeRef(sym.typeConstructor.prefix, sym, elems) - } else NoType - } - lazy val ProductRootClass: Symbol = getRequiredClass("scala.Product") def Product_productArity = getMember(ProductRootClass, nme.productArity) def Product_productElement = getMember(ProductRootClass, nme.productElement) @@ -616,18 +614,6 @@ trait Definitions extends reflect.api.StandardDefinitions { /** returns true if this type is exactly ProductN[T1,...,Tn], not some subclass */ def isExactProductType(tp: Type): Boolean = isProductNClass(tp.typeSymbol) - def productType(elems: List[Type]) = { - if (elems.isEmpty) UnitClass.tpe - else { - val len = elems.length - if (len <= MaxProductArity) { - val sym = ProductClass(len) - typeRef(sym.typeConstructor.prefix, sym, elems) - } - else NoType - } - } - /** if tpe <: ProductN[T1,...,TN], returns List(T1,...,TN) else Nil */ def getProductArgs(tpe: Type): List[Type] = tpe.baseClasses find isProductNClass match { case Some(x) => tpe.baseType(x).typeArgs @@ -640,21 +626,10 @@ trait Definitions extends reflect.api.StandardDefinitions { } def functionApply(n: Int) = getMember(FunctionClass(n), nme.apply) - def functionType(formals: List[Type], restpe: Type) = { - val len = formals.length - if (len <= MaxFunctionArity) { - val sym = FunctionClass(len) - typeRef(sym.typeConstructor.prefix, sym, formals :+ restpe) - } else NoType - } - def abstractFunctionForFunctionType(tp: Type) = tp.normalize match { - case tr @ TypeRef(_, _, args) if isFunctionType(tr) => - val sym = AbstractFunctionClass(args.length - 1) - typeRef(sym.typeConstructor.prefix, sym, args) - case _ => - NoType - } + def abstractFunctionForFunctionType(tp: Type) = + if (isFunctionType(tp)) abstractFunctionType(tp.typeArgs.init, tp.typeArgs.last) + else NoType def isFunctionType(tp: Type): Boolean = tp.normalize match { case TypeRef(_, sym, args) if args.nonEmpty => @@ -671,17 +646,21 @@ trait Definitions extends reflect.api.StandardDefinitions { case _ => NoType } - def seqType(arg: Type) = appliedType(SeqClass.typeConstructor, List(arg)) - def arrayType(arg: Type) = appliedType(ArrayClass.typeConstructor, List(arg)) - def byNameType(arg: Type) = appliedType(ByNameParamClass.typeConstructor, List(arg)) - def iteratorOfType(tp: Type) = appliedType(IteratorClass.typeConstructor, List(tp)) + def arrayType(arg: Type) = appliedType(ArrayClass, arg) + def byNameType(arg: Type) = appliedType(ByNameParamClass, arg) + def iteratorOfType(tp: Type) = appliedType(IteratorClass, tp) + def javaRepeatedType(arg: Type) = appliedType(JavaRepeatedParamClass, arg) + def optionType(tp: Type) = appliedType(OptionClass, tp) + def scalaRepeatedType(arg: Type) = appliedType(RepeatedParamClass, arg) + def seqType(arg: Type) = appliedType(SeqClass, arg) + def someType(tp: Type) = appliedType(SomeClass, tp) - lazy val StringArray = arrayType(StringClass.tpe) + def StringArray = arrayType(StringClass.tpe) lazy val ObjectArray = arrayType(ObjectClass.tpe) def ClassType(arg: Type) = if (phase.erasedTypes || forMSIL) ClassClass.tpe - else appliedType(ClassClass.typeConstructor, List(arg)) + else appliedType(ClassClass, arg) def vmClassType(arg: Type): Type = ClassType(arg) def vmSignature(sym: Symbol, info: Type): String = signature(info) // !!! @@ -940,7 +919,7 @@ trait Definitions extends reflect.api.StandardDefinitions { ) lazy val AnnotationDefaultAttr: Symbol = { - val attr = enterNewClass(RuntimePackageClass, tpnme.AnnotationDefaultATTR, List(AnnotationClass.typeConstructor)) + val attr = enterNewClass(RuntimePackageClass, tpnme.AnnotationDefaultATTR, List(AnnotationClass.tpe)) // This attribute needs a constructor so that modifiers in parsed Java code make sense attr.info.decls enter attr.newClassConstructor(NoPosition) attr @@ -1085,10 +1064,10 @@ trait Definitions extends reflect.api.StandardDefinitions { private lazy val boxedValueClassesSet = boxedClass.values.toSet + BoxedUnitClass /** Is symbol a value class? */ - def isPrimitiveValueClass(sym: Symbol) = scalaValueClassesSet(sym) + def isPrimitiveValueClass(sym: Symbol) = ScalaValueClasses contains sym def isNonUnitValueClass(sym: Symbol) = isPrimitiveValueClass(sym) && (sym != UnitClass) def isSpecializableClass(sym: Symbol) = isPrimitiveValueClass(sym) || (sym == AnyRefClass) - def isScalaValueType(tp: Type) = scalaValueClassesSet(tp.typeSymbol) + def isScalaValueType(tp: Type) = ScalaValueClasses contains tp.typeSymbol /** Is symbol a boxed value class, e.g. java.lang.Integer? */ def isBoxedValueClass(sym: Symbol) = boxedValueClassesSet(sym) |